Using DiffUtil In A RecyclerView

Florence
3 min readApr 7, 2022

A recycler view is a view group that displays a list of data to the user. It comes from the word recycle since it recycles or reuses views to show new data once the user scrolls.

A recyclerview has three main components:

  1. Layout: This component displays a list to the users. There are three different layouts:
  • Linear Layout Manager — It shows lists in a horizontal or vertical orientation.
  • Grid Layout Manager — It shows lists in a grid orientation.
  • Staggered Layout Manager — This is a grid view where each view has different height measurements.

2. An Adapter: This class collects data and binds it to a view holder before displaying it to the UI.

3. Item Animator — It displays animations as changes occur in the adapter

Data displayed by the recyclerview is constantly changing due to the deletion of data and the addition of new data. To observe these changes in the UI, we have to call notifyDataSetChanged.

notifyDataSetChanged recreates all the views and then displays a list with the most recent changes; notifyDataSetChanged works well, but for a recyclerview with a high number of items, the process will consume a lot of time and might cause your app to freeze. To solve this problem, DiffUtil was created.

DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one. DiffUtils is based on Eugene Myers’ algorithm. -Android Official Documentation

DiffUtil has a callback class called DiffUtil.callback that computes the variation between two lists. It implements the following methods.

Implementing DiffUtil in a RecylerView

We will create a recyclerview that displays a list of Disney character names and their images.
First, we will create the main activity layout XML.

We will create an item layout for our recylerview.

We will then create our adapter class. In the adapter class, we will declare a diffUtil variable and assign it to a DiffUtil.ItemCallback and implement its member classes;

areItemsTheSame: will help us check if our items in the current list and new list are identical

areContentsTheSame: will check if the items in the two lists have similar data

We will use AsyncListDiffer, a DiffUtil helper that calculates the difference between two lists on a background thread. After calculating the difference, it signals the adapter of the changes in the two lists.

We will create a function saveData() that will be called in the main activity to set data to the recyclerview layout.

The overall adapter class will look like this

In the Main Activity, we will set up the adapter and the layout manager and set the data to the adapter.

Finally, our recyclerview will appear like this when we run the app.

In case new Disney characters are added to our list from the API endpoint, Diffutil will calculate the difference between the current list and the latest list and update the changes in our current list without recreating views.

DiffUtil will boost the app’s performance, especially when the number of items are many. Check out the diff util performance results here.

Check out the complete code from Github.

--

--