How to Update/Refresh Specific Item in Recyclerview

How to update/refresh a specific item of RecyclerView without calling notifiyItemChanged or DiffUtil?

You can try with notifyItemChanged()

notifyItemChanged(int position, Object payload)

Notify any registered observers that the item at position has changed
with an optional payload object.

LOGIC

arrayList.set(updatePosition, yourValue);
adapterOBJ.notifyItemChanged(updatePosition);

Update Specific item in recyclerview

Answer : ( Update Specific item in recyclerview )

notifyItemChanged(Adapterposition) , Work fine , and here is some description

notifyItemChanged method tell OnBind To redraw item at that position,
Your must consider update arrayData at that Position to reflect new changes before calling this method

      versionViewHolder.ClickedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayDataAt[position] = " New Data to replace Old one ";
notifyItemChanged(Adapterposition);
}
});

What else if you want to change something that doesn't come from array data set ?

This is exactly like in my case , ( and the same as to update background color , text colors etc .

You can create an identifier ( simply a boolean array ) which contain status of your rows , let say all rows will have a boolean value false , when a row is click update booleanArray at that position to be true , remember to tell on bind to consider true element with specific operation like different colors , different font , etc , and finally notify notifyItemChanged(Adapterposition);

So what was wrong with my code

  1. Onclick event changes appear , but after scrolling away from that view and back again OnBind Keep on Recreating the view with the same Data as old ones . ( its important to either update the array data at position or to have a flag that show data have been updated )

  2. My view come with drawable item which i never update on bind , so Onclick , those items that appear to be at the same UI with clicked item will also reflect changes . ( There was mixed concept of dynamic view element and static view element )

What else if someone data come from SQLite?

Consider to put your data in Array first .

Onclick Update your SQLite data in background and update view to reflect that data (Don't waste memory on redraw the whole recyclerview from SQLite again)

Thanks to all contributers .

how to refresh recyclerview specific item view

Thanks for your help, I have find the problem
the problem is not in adapter, it in my viewholder
my xml layout show the icon in default,

when it is the last item ,I set the icon Gone

public static void hideBottomLine(BaseViewHolder h) {
h.line.setVisibility(View.GONE);
}

when i notifyItemChanged it ,I thought it will show default, but not! So when I add method

public static void showLine(BaseViewHolder h) {
h.line.setVisibility(View.VISIBLE);
}

now it work...

How to update single item in recyclerview?

Your item is clone because you are adding item.

Try to use set instead of add like this

public void setItemToPostion(WatchListEpisodeBean watchListBean, int itemPosition) {
this.watchListBean.set(itemPosition, watchListBean);
notifyItemChanged(itemPosition);
}

RecyclerView Update Single Item's Child

Let's say you have a class named User and you are passing an ArrayList(that contains different User objects) to the RecyclerView Adapter.

public class User
{
....
private String name;
private int age;
//Getters, setters, etc ....
}

So for updating just the age, simply update that object present at that specific position in the list and then just refresh the RecyclerView.

....
users.get(position).setAge(xyz);
userAdapter.notifyItemChanged(position);
....

How to update RecyclerView Adapter Data

I'm working with RecyclerView and both the remove and the update work well.

  1. Remove:

    There are four steps to remove an item from a RecyclerView

     list.remove(position);
    recycler.removeViewAt(position);
    mAdapter.notifyItemRemoved(position);
    mAdapter.notifyItemRangeChanged(position, list.size());

    These lines of code work for me.

  2. Update the data:

    The only things I had to do was:

     mAdapter.notifyDataSetChanged();

You had to do all of this in the Actvity/Fragment code, not in the RecyclerView Adapter code.



Related Topics



Leave a reply



Submit