An easiest way to update RecyclerView

Vandana Srivastava
3 min readMay 21, 2021

--

You all are using smartphones and lots of apps so almost in all the apps has ListView or RecyclerView as it is the main UI component to represent huge list of data in Android. But, do you know the easiest way to update list in a recyclerView?

Today, I decided to write about it. But before writing, I want you to think one question: ‘Why do we need easy way to update RecyclerView?’.

Let me tell you, as an android developer we are already familiar with ListView, which was too heavy class and has a lot of performance issues. To overcome this problem RecyclerView has introduced. RecyclerView has a lot of things which ListView is lacking in it. Thus, improve performance.

But, notifyDataSetChanged() is the common notifier used in both. But, it also has some drawbacks:

  • NotifyDataSetChange is a very expensive process.
  • It recreates the entire ViewHolder and refreshes the entire view if a new item has been introduced in a list. This process sometimes causes problems with the list auto scroll or the list may flicker. So, to overcome this problem DiffUtil has introduced.

DiffUtil

DiffUtil is the optimised way to show data in a RecyclerView. This utility class has written an algorithm (Eugene w. Myers Algorithm) in such a way that it takes the list as an input and compares the difference between them by calculating the minimal number of updates. And update the first list into a second list. It updates only those content in an adapter which needs to change, not the entire ViewHolder.

Let’s understand with an example:

  • Suppose you have 2 lists of Sports such as oldSports and newSports.
  • DiffUtil fetches most basic details from both the lists oldSports and newSports, like the size and basic item comparison and this basic information is provided by the DiffUtil.Callback which DiffUtil going to compare

val diffResult = DiffUtil.calculateDiff(diffCallback) diffResult.dispatchUpdatesTo(adapter.context)

DiffUtil calculate difference DiffUtil.calculateDiff(diffCallback) method and the difference result dispatchUpdate to adapter context, the adapter updates that particular changed item.

DiffUtil.Callback is an abstract class and have 5 override methods :

  1. getOldListSize(): As name suggests, returns the size of the old list.

2. getNewListSize(): same, returns the size of the new list.

3. areItemsTheSame(int oldItemPosition, int newItemPosition) : This method compares each item in the old list and the new list by checking whether individual item’s ids(primary key) are the same or not by returning a boolean value i.e. true and false.

4. areContentsTheSame(int oldItemPosition, int newItemPosition): This method checks whether contents(data in each item) of the old list and new list are the same or not and return boolean accordingly

5. getChangePayload(int oldItemPosition, int newItemPosition): This method is called by the DiffUtil API only if areItemsTheSame returns true and areContentsTheSame returns false. This method returns an object that contains the data change payload. From this method, we can detect the change in data and pass the changed data to the recycler adapter in the form of a bundle object.

Below is just preview of DiffUtilCallback class.

For complete source code, you can check out below repository link.

So, if I talk about DiffUtil advantage:

  • It is fast in performance. It has O(N + D²) expected time performance where D is the length of the edit script.
  • Consumes less memory space O(N) to find the minimal number of addition and removal operations between the two lists
  • If move detection is enabled, it takes an additional O(MN) time where M is the total number of added items and N is the total number of removed items.

Note: The DiffUtil class compares two lists, the old List and new List. But this process is heavy and we should not do this on the main thread. So AsyncListDiffer class do this job asynchronously. One thing should keep in mind , list passed into asyncListDiffer should not be changed or its elements should not change directly. Instead, we have to pass a new list every time we make any change to the old list .

Here are few more resources to learn about DiffUtil:

  1. Official Documentation of DiffUtil
  2. Advanced use of DiffUtil using RxJava

That’s all for now. If you are interested more in Android and related stuffs

don’t forget to follow me on LinkedIn and Twitter.

Please show some love by clapping this post and give star to Github repo.

Happy Learning!

--

--