Add a new item to recyclerview programmatically?
First add your item to mItems
and then use:
mAdapter.notifyItemInserted(mItems.size() - 1);
this method is better than using:
mAdapter.notifyDataSetChanged();
in performance.
Adding Some View to RecyclerView item programmatically creates an known bug
Recycler recycle views :) - It uses same view to show multiple data! That means that a view can be used to show an object with stars - so on onBindViewHolder
you show the stars. And then the same view (with stars now) will be used to show a different object (no star object) - it means that on onBindViewHolder
you will now need to hide the stars, which I bet you didn't ;)
As I said, if you have no stars you don't remove the previous starts :
if (task.priorityStars == 0)
return
Change your code to:
@BindingAdapter("app:setPriority")
fun LinearLayoutCompat.setPriority(task: ToDo) {
val previousStarContainer = findViewWithTag<LinearLayout>("starContainerLayout")
if (previousStarContainer != null) {
removeView(previousStarContainer)
}
if (task.priorityStars == 0)
return
val starContainer = LinearLayout(context)
starContainer.tag = "starContainerLayout"
starContainer.layoutParams = LinearLayoutCompat.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
for (count in 1..task.priorityStars) {
val starView = ImageView(context)
starView.setImageResource(R.drawable.task_star)
starView.layoutParams = LinearLayoutCompat.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
starContainer.addView(starView)
}
addView(starContainer)
}
Adding Items in RecycleView Dynamically using a button
Add this method to your adapter and call on button click.
public void newAddeddata(String company_name){
NewleadsPOJO newValue=new NewleadsPOJO();
newValue.setLeads_company(company_name);
datalist.add(newValue);
notifyDataSetChanged();
}
Add following method to NewLeadFrag
public NewleadsAdapter getAdapter(){
return adapter;
}
now in Createmeetingfrag
leads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NewLeadFrag fragment = getFragmentManager().findFragmentByTag("NewLeadFrag_TAG"); //set tag of fragment when you add with fragment manager. and if you are using support library use getSupportFragmentManager()
if(fragment!= null){
fragment.getAdapter().newAddeddata(company_name.getText().toString());
}
}
});
Recycler view programmatically modify item at given position
In order to modify the background of a list item in the RecyclerView
, you'll have to do the following:
modify the
RecyclerView.ViewHolder
so it will change the background of the view according to the data passed to it inRecyclerView.Adapter.onBindViewHolder
.modify
RecyclerView.Adapter.onBindViewHolder
to pass data to theRecyclerView.ViewHolder
according to how dark the background should be.call
notifyDataSetChanged()
on yourRecyclerView.Adapter
to update theRecyclerView
on the GUI.
Best method for adding items to a recyclerview from another activity?
your need to call adapter.notifyDataSetChanged()
after add an item into adapter
Best practice in this case is startActivityForResult()
from MainActivity
In the SecondActivity, pass the extra item you want to add in MainActivity back to MainActivity
In the MainActivity, override onActivityResult()
and handle adding the extra item into the adapter and call adapter.notifyDataSetChanged()
How to add a static item, eg Add new contact, always as the first item of the recycler view?
You should not add that "add new customer" option as an element of the RecyclerView. That should simply be a TextView added before the RecyclerView in vertical LinearLayout. Attach a click listener, and add the desired logic to it.
Related Topics
Align Top of Image to Top of Textview
Server-Side Verification of Google Play In-App Billing Version 3 Purchase
Single Observable with Multiple Subscribers
Capture Picture Without Preview Using Camera2 API
Analyticsservice Not Registered in the App Manifest - Error
Android Toolbar: Small Title Text in Landscape Mode
Tiled Drawable Sometimes Stretches
How to Extract the File Name from Uri Returned from Intent.Action_Get_Content
Why There's a Separate Mutablelivedata Subclass of Livedata
How to Pass Parameters to Onclicklistener
Android Emulator VS Real Device
How to Retrieve More Than 1000 Rows from Parse.Com
Post on Facebook Wall Using Facebook Android Sdk Without Opening Dialog Box
Change Navigationview Items When User Is Logged
How to Add Custom Font in React Native Android