Animate RecyclerView items update

Avalon Joshua
2 min readMar 19, 2020

--

RecyclerView is common widget used in Android. It has build-in methods to handle insert, change, and removed. If you uses those methods, RecyclerView will animate it accordingly instead of refresh the whole list.

  • Insert: Items after the inserted item move down.
  • Update: Updated item changes content instead of view refresh.
  • Remove: Items after the removed item move up.

Prerequisite

  • Able to create a working RecyclerView

The old fashion way

RecyclerView.Adapter has notifyDataSetChanged(), notifyItemChanged(int), notifyItemInserted(int), notifyItemRemoved(int), notifyItemRangeChanged(int, int), notifyItemRangeInserted(int, int), notifyItemRangeRemoved(int, int).

notifyDataSetChanged() is the simplest way to notify the adapter that the data has changed. However, adapter do not know what has changed, so it will refresh all the views. Many programmer will just use notifyDataSetChanged() after updating the list.

In order to allow RecyclerView to animate item update, changes must be notified specifically. There are two ways to do it:

First one is to extend existing list class and dispatch changes on different methods. While this is the efficient to dispatch changes, it requires all the methods to be extended, including method like sort and filter.

Second one is comparing current list and new list to calculate the different and dispatch changes accordingly. While this is less efficient, it is a more generic way to handle all the cases.

However, both ways have to implement it yourself.

The new way

DiffUtil is added in version 25.1.0 which has implemented the second way for you.

DiffableListCallback is an implementation of DiffUtil.Callback which contains logic on how to compare the items.

Two items are the same if they are refer to the same item. For example, in terms of database, it means they have the same primary key.

Two items are content same if they are equal in value. If two item are equal in value, it means there are no needs to update the item.

Therefore, Diffable delegates the comparison logic to the item. Combine Diffable with data class which implements value equality, you only need to implement isSame.

After you have update the list, you can just update the items in the adapter and DiffUtil will calculate the changes for you.

Originally published at https://joshuaavalon.io on March 19, 2020.

--

--