Android RecyclerView DiffUtil With Examples

In this tutorial, we’ll be discussing and implementing DiffUtil in our android application. If you haven’t implemented RecyclerView yet, refer this tutorial before proceeding ahead.

Android RecyclerView DiffUtil

DiffUtil is a utility class present with the Android Support Library. It’s a callback that detects changes between two Lists. It’s used as an alternative for notifyDataSetChanged() in our RecyclerView adapters.

notifyDataSetChanged is costly. Whenever invoked it updates each and every RecyclerView row. This is costly. DiffUtil does not call each and every row. Instead, it updates only those rows that are different between the Lists of values.

DiffUtil is a utility class that can calculate the difference between two lists and output a list of operations that converts the first list into the second one.

DiffUtil uses the following methods of the RecyclerViewAdapter:

  • notifyItemMoved()
  • notifyItemRangeChanged()
  • notifyItemRangeInserted()
  • notifyItemRangeRemoved()

These are less costlier than notifyDataSetChanged since they work on individual operations.

DiffUtil.CallBack class has the following methods that need to be implemented:

  • getOldListSize() : It returns the size of the old list.
  • getNewListSize() : Returns the size of the new list.
  • areItemsTheSame(int oldItemPosition, int newItemPosition) : We check if the individual items of the list are same. This can be done through checking their ids.
  • areContentsTheSame(int oldItemPosition, int newItemPosition): This checks if the contents of the List data are the same. This method is called by DiffUtil only if areItemsTheSame returns true.
  • getChangePayload(int oldItemPosition, int newItemPosition) : If areItemTheSame return true and areContentsTheSame returns false DiffUtil calls this method to get a payload about the change. Here we can detect if any particular field of the data is changed. We can then pass that changed value using Bundle. It’ll be received in our RecyclerView Adapter class.

Enough theory, let’s learn it by example now.

In the following section, we’ll create an application that shows dummy data of CryptoCurrency coins in our RecyclerView. Using two FloatingActionButtons, we can alter the data (add more data, modify current data).

Let’s dive right in!

Project Structure



The code for the activity_main.xml layout is given below:

The code for the cardview_item_layout.xml file is given below:

This hosts the RecyclerView rows.

Let’s look at our class:

We’ve implemented the Comparable and Cloneable Interfaces.

They’ll be used to compare the objects in the DiffUtil and to create a deep copy of the ArrayList of Models that we’ll see next.

The code for the class is given below:

ArrayList are passed by reference. We need to create a deep copy of the ArrayList to prevent the original ArrayList from changing when we modify the new ArrayList. Otherwise, DiffUtil would get two ArrayLists that are just the same.

recyclerViewAdapter.setData(models); is used to set the new data in the Adapter class.

The code for the class is given below:

onBindViewHolder(CryptoViewHolder holder, int position, List<Object> payloads) gets triggered when the DiffUtil makes changes in the Adapter.

Inside it, we get the bundle value from the payload of the DiffUtil class. This contains the individual fields that were changed.

Inside the setData() we pass the new ArrayList.

dispatchUpdatesTo(this) invokes the adapter and informs it about the views to be updated.

Next, we copy the newData list to the data.

The code for the class is given below:

“price” is the payload data that was passed to the Adapter.

We can then animate/change text color of the field to indicate the user that it was modified.

The output of the above application in action is given below:

android recyclerview diffutil output

Here we’ve changed the text color of the rows in which the price was modified.

It’s just a random data. Wish the actual crypto market looks like this someday too!

This brings an end to this tutorial. You can download the project from the link below:

By admin

Leave a Reply

%d bloggers like this: