Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView - how to update and move item at the same time

I have a RecyclerView Adapter backed by a SortedList. If I make a change to an item, it both changes the item and repositions it in the list.

I've found that if I use notifyItemChanged on either the item's starting or ending position, it does not seem to have any effect even in conjunction with notifyItemMoved, either before or after.

If I use notifyItemMoved, it correctly triggers the movement animation, but the view does not change and still displays the outdated information.

If I use notifyDatasetChanged it updates the row and then moves it, but it does so sequentially which is slow, and it obviously notifies the entire list which is not exactly desirable.

Is there any way I can combine the moving and updating animations? And why doesn't notifyItemChanged do anything?

like image 663
TBridges42 Avatar asked Mar 05 '17 23:03

TBridges42


2 Answers

In RecyclerView.Adapter reference is said, that notifyItemMoved() is just structural change and therefore won't update data. On the other hand notifyItemChanged() is said to be data change.

When calling notifyItemChanged(), it will call RecyclerView#onBindViewHolder(), so it should update your view.

Working approach for me for updating and moving is:

notifyItemChanged(oldPos); notifyItemMoved(oldPos, newPos);

like image 115
mlykotom Avatar answered Sep 28 '22 15:09

mlykotom


You can use:

SortedList.updateItemAt(int position, Objet newItem)

The newItem is the updated item, and position is the current position. This method replaces the current item for newItem and repositions it on the list (and the recyclerview link to it).

Here is the official documentation.

I hope this helps you.

like image 20
J. Manuel Avatar answered Sep 28 '22 15:09

J. Manuel