DiffUtil in RecyclerView in Android
Have you ever created a List in Android? What did you use to make it? ListView or RecyclerView are two types of views. If you are an Android Developer it’s sure you’ve used RecyclerView at some point. In this article, we’ll go through how to update the RecyclerView with DiffUtils. What exactly is RecyclerView? RecyclerView is a more adaptable and efficient version of ListView. It is a container for displaying a larger data set of views that can be recycled and scrolled very quickly. Before we go into Diff Util, let’s have a look at the RecyclerView implementation.
RecyclerView Implementation in Brief
Let’s make an Activity MainActivity and include the following code in the activity main.xml file:
Let’s make a data model class and a Data Source now,
and the data source appears to be,
Let’s make an adaptor now to set the list in RecyclerView.
What if we need to update the list with fresh information?
We’ll refer to it as,
and make a call from MainActivity,
GeekTip #1: The recyclerview will be updated with the new set of data as a result of this.
But, since notifyDataSetChanged was performing the work for you, why did you require DiffUtils? Let’s talk about it.
- There is no way for the RecyclerView to know what the real changes are if notifyDataSetChanged() is used. As a result, all visible views are rebuilt. This is an extremely costly surgery.
- During this procedure, a new instance of the adapter is generated. As a result, the process is highly time-consuming.
To address this, Android introduced DiffUtils as part of its support library.
It’s a utility class that helps us to perform complex tasks easily, Eugene Myers’ algorithm is the foundation of DiffUtils.
DiffUtils is used to track changes made to the RecyclerView Adapter. DiffUtil notifies the RecyclerView of any changes to the data set using the following methods:
These techniques are significantly more efficient than notifyDataSetChanged(). However, in order for DiffUtils to function in the project, we must give information about the old and new lists. DiffUtil is used for this. Request a callback. We’ll make a class.
- getOldCourse(): This function returns the length of the old list.
- getNewCourse(): Returns the new list’s size.
- areItemsTheSame(oldPosition:Int, newPosition:Int): Called by the DiffUtil to determine whether two objects in the old and new lists represent the same Item.
- areContentsTheSame(oldPosition:Int, newPosition:Int): Determines if two objects have the same data. Depending on your UI, you may modify its behavior. DiffUtil calls this function only if areItemsTheSame returns true. In our example, we’re contrasting the name and price of a certain item.
- obtaingeeksPayload(oldPosition:Int, newPosition:Int): If areItemTheSame returns true and areContentsTheSame returns false, the condition is satisfied. Diff This method is called by Util to obtain a payload about the modification.
To utilize this, we change the setData function in Adapter.
What are the Benefits of using DiffUtils?
The performance chart below shows that using DiffUtil is superior in the case of RecyclerView. These findings are based on the Nexus 6P with M-
- 100 items and 10 modifications: average: 0.39 milliseconds, median: 0.35 milliseconds
- 3.82 ms for 100 items and 100 modifications, 3.75 ms for the median.
- 2.09 ms for 100 items and 100 changes without movements, with a median of 2.06 ms.
- 1000 items and 50 modifications: average: 4.67 milliseconds, median: 4.59 milliseconds
- 1000 things and 50 changes with no moves: 3.59 ms on average, 3.50 ms on average
- 1000 items and 200 modifications: 27.07 milliseconds, median: 26.92 milliseconds
- 1000 items and 200 changes without moves: 13.54 milliseconds, median: 13.36 milliseconds
Because of the specified limitation, the maximum size of the list is 226. This is how DiffUtils may be used to update the list in RecyclerView.