Learn about DiffUtil and ListAdapter

Tram Ho

1. Introduction to DiffUtil

RecyclerView is a ViewGroup introduced in Android L (Android API 21) supported in support-v7 version . With so many new powerful features than the ListView predecessor. One outstanding feature is Notify data not only unique notifyDataSetChanged () but also more like notifyItemChange () , notifyItemMoved () , notifyItemInserted () very convenient for us to handle when there is a change somewhere on list.

But in some cases you have a large list, but doing a lot of complicated tasks on it is not easy to know which element should notify the RecyclerView and update it correctly, then call notifyDataSetChanged ( ) to indicate that all data has changed, low application performance reduces user experience. But don’t worry there is a utility class called DiffUtil that will help us do that.

Go to version 24.2.0, RecyclerView support library, v7 package provides the class called DiffUtil . It basically looks for the difference between the two lists and provides the output as the update list, which is used to notify the update to a RecyclerView adapter. Basically the DiffUtil still uses the RecyclerView.Adapter methods to notify the adapter to update the data as

  • notifyItemChange ()
  • notifyItemMoved ()
  • notifyItemInserted ()

And use Eugene W. Myers’ algorithm to calculate the minimum number of updates

Deploy DiffUtil

Functions to note

  • getOldListSize (): It returns the size of the old list.
  • getNewListSize (): Returns the size of the new list.
  • areItemsTheSame (int oldItemPosition, int newItemPosition): Check if the individual items of the list are the same. This can be done through checking them id
  • areContentsTheSame (int oldItemPosition, int newItemPosition): Check if the content of the list data is the same. This method is called by DiffUtil only when areItemsTheSamet returns true.
  • getChangePayload (int oldItemPosition, int newItemPosition): If areItemTheSame returns true and areContentsTheSame returns false, DiffUtil calls this method to return the change

UserDiffUtil.class

And deploy in Adapter

UserAdapter.class

Multi-Thread and Service in Android
Basic about Android App Bundle

2. Introduction to ListAdapter

From the effects that DiffUtil brings, in support library 27.1.0 ListAdapter is a wrapper of RecyclerView.Adapter to help simplify the code needed to work with RecyclerView and can automatically store the old List Item and Use DiffUtil to only update items that have changed.

Let’s also see how the normal code we usually do

How to use the old Adapter

Here’s how to use the new Adapter

Whenever you need to update the adapter, you can provide a new item list so that it uses the submitList (List) method.

3. Summary

The above is my basic tutorial on DiffUtil and ListAdapter basically, it is quite similar to deploying the old RecyclerView.Adapter as a bridge between them to create flexibility as well as performance when you handle the lesson. big math when there is a change.

Reference source:

https://www.journaldev.com/20873/android-recyclerview-diffutil?fbclid=IwAR3FzotH0rtTTSUkF386DmOhWI3Ds4VY7yVp6R6o8dCdAT1IVs7vC3eSiZw

https://medium.com/@trionkidnapper/recyclerview-more-animations-with-less-code-using-support-library-listadapter-62e65126acdb?fbclid=IwAR0-vTtj2naxa_sHrnEhRMFt1M-xDljVDSvTFo7xLl5KCFaqnXlEKU3_5PY

Introducing VisBug tool, Google owner made the life of FrontEnd Designer less miserable
Introducing LevelDB and comparing performance with MySQL
Share the news now

Source : viblo