Elye Elye - 2 years ago 155
Android Question

Animation on RecyclerView when performing multiple insert/removal (not in single range)


, when we insert one or remove an item, we could have it animate in using the below API:

// within the adapter
// or

Even for a range that change, we could use

// or

However, if I have multiple items inserted/removed, but they are not in a range, is it possible to still have the needed animation?

e.g. I'm inserting A, B, D, G ... into a list of C, E, F, H... hence I get A, B, C, D, E, F, G, H.... where the appropriate animation should show A, B and D and G inserted, while C, E, F and H animate down/up to allow the feel of insertion. Just like the single insertion/removal, but done with multiple item in parallel.

I'm okay, if a Library could help perform that as well.

I don't think https://github.com/wasabeef/recyclerview-animators and https://github.com/gabrielemariotti/RecyclerViewItemAnimators could achieve that. I hope I am wrong though.

Answer Source

You can dispatch as many of them as you want. RecyclerView batches all adapter changes until the next layout pass and executes them together.

so in your case of inserting A, B, D, G into C, E, F, H to get A, B, C, D, E, F, G, H, you can dispatch:

notifyItemRangeInserted(0, 2); //insert A,B, list becomes: ABCEFH

notifyItemInserted(3); //insert D, list becomes: ABCDEFH

notifyItemInserted(6); // insert G, list becomes: ABCDEFGH

And RecyclerView will animate all of them together.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download