Problem with updating row view in recyclerview

I want to update the recyclerview view when calling notifyItemChanged. The fact is that I do not want to update the entire line, but only the representation of the line. (to avoid flashing line effect)

There is a method called notifyItemChanged (int, obj payload).

Can I use this to achieve this? If so, how to do it?

+6
source share
3 answers

Finally, I found how to update only a specific row view in RecyclerView.

(1) Override onBindViewHolder (Recycler.ViewHolder VH, int position, Payload List) in the adapter

(2) Inside the onBindViewHolder method

if(payloads != null && !payloads.isEmpty() && (payloads.get(0) instanceof customObject)){ // update the specific view }else{ // I have already overridden the other onBindViewHolder(ViewHolder, int) // The method with 3 arguments is being called before the method with 2 args. // so calling super will call that method with 2 arguments. super.onBindViewHolder(holder,position,payloads); } 

(3) To notify about data changes and update the view, you need to call notifyItemChanged (position int, payload Object) . You must pass customObject (the model that stores the data) as the payload object.

 adapter.notifyItemChanged(i, obj); 

Note. You must disable the ItemAnimator RecyclerView element (it is enabled by default). Otherwise, the payload object will be empty.

 recyclerView.setItemAnimator(null); 

or

 ((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 

UPDATE: I used this method recently without disabling ItemAnimator. Therefore, there is no need to disable ItemAnimator. - (recyclerview-v7: 25.4.0)

for further links

+12
source

You can use this to get a view of the updated item:

 View v = recyclerView.getLayoutManager().findViewByPosition(position); if (v != null){ //update your view } 
+3
source

Let's say you have an ArrayList of elements , and it feeds into the RecyclerView adapter. Let it be something like this.

 List<String> items = new ArrayList<>(); RecyclerAdapter adapter = new RecyclerAdapter(items); recyclerView.setAdapter(adapter); 

Now, as I see it, you want to insert elements into the RecyclerView and call the notifyItemChanged (int position) method to add only one element.

To do this, suppose you change the value of an item at position 0. This is the first item in the list.

Now the correct use of the notifyItemChanged (int position) method will be

 adapter.notifyItemChanged(0); 

This means that the adapter will update the recyclerview with the new value at position 0.

-1
source

All Articles