Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate item-swap in RecyclerView

I've implemented a recycler view with an adapter and everything. I can reverse the list of items with

Collections.reverse(items);

and then run through the list to notify the adapter of this change

final int end = items.size() - 1;
for (int i = 0; i < end; i++) {
    mAdapter.notifyItemMoved(0, end - i);
}

So far so good. What I want to do now is to swap two items in that list and animate this change.

Collections.swap(items, 5, 8);

mAdapter.notifyItemMoved(5, 8);
mAdapter.notifyItemMoved(8, 5);

The problem is, this is not being animated and I couldn't find out why.

I do want something similar to the simple reverse animation.

Any ideas?

like image 967
Alex Avatar asked Nov 01 '22 10:11

Alex


1 Answers

Soon after I wrote this I hacked a little more and found the reason. Turns out, my problem does relate to the following thread:

No animation on item removal on RecyclerView

I did mess up my notifyDataSetChanged() and notifyItemMoved(0, 1) calls. Once notifyDataSetChanged() is called, the items are swapped without an animation and notifyItemMoved has no effect.

like image 92
Alex Avatar answered Nov 08 '22 10:11

Alex