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

TBridges42 picture TBridges42 · Mar 6, 2017 · Viewed 8.6k times · Source

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?

Answer

mlykotom picture mlykotom · Jul 29, 2017

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);