Add a New Item to Recyclerview Programmatically

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:

  1. modify the RecyclerView.ViewHolder so it will change the background of the view according to the data passed to it in RecyclerView.Adapter.onBindViewHolder.

  2. modify RecyclerView.Adapter.onBindViewHolder to pass data to the RecyclerView.ViewHolder according to how dark the background should be.

  3. call notifyDataSetChanged() on your RecyclerView.Adapter to update the RecyclerView 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



Leave a reply



Submit