DiffUtil in multiple columns list with different items

Alexey Kuznetsov
Dev Genius
Published in
3 min readFeb 8, 2022

--

Photo by Ine Carriquiry on Unsplash

Hello! Today I’ll talk about how to implement DiffUtil in your project and in case for list with multiple columns. Here the final result 👇

If you’re wondering: “Hey, how can I create such list with multiple columns?”, I have an answer… 😅 Check out my previous article about it.

About DiffUtil | For what is it?

This useful thing hides inside RecyclerView library. It’s need to compare two lists (previous and new one) and call RecyclerView.Adapter notify functions for every difference. Moreover, it has advantages before old way used functions:

🔹Better performance
🔹Less code
🔹Beautiful animations played together

I think that also answer the question: “Why we should use DiffUtil?” 😇

How to start?

Mainly we will work with Adapter classes, make some transformation with them, and create some new. Firstly, let’s create a new parent class for all Adapters, which will implement DiffUtil.

Some important notes about this class:

  • Variable diffResult is the result of library work (calculation of difference between two lists).
  • Abstract variable diff (with parent ParentDiff<T>) busy with comparing items and how to identify them in list.
  • You may note comment above setList function. It means (for class realization) that need copy items of list (which is parameter) inside this.list. Just set new list - it’s not enough. It will prevent changing inside DiffUtil list (if some changes happened in list you store) and make animation work the right way.

Below introduced parent class realization. It’s not full because of similar code with previous article. Pay attention to setList functions (again 😃), not all items copy here (skip object items with static data).

Full code you may find in GitHub (link to project at the end of article)

Article update (22.02.22)

Recently I discover for myself a class ListAdapter which really similar to ParrentDiffAdapter. But it’s better because calculate difference between lists on background thread. In documentation page of ListAdapter you may find how to implement this class.

Compare class

Now, about ParentDiff and its child — TestDiff. It’s all realizations of DiffUtil.Callback. Base on this callback realization in adapter will be called notify functions. Here what documentation says about it:

A Callback class used by DiffUtil while calculating the diff between two lists.

Generic item - as in DiffAdapter

Few comments about important moments:

  • areItemsTheSame — this function to identify the same objects (by id for example) in different lists.
  • areContentsTheSame — this function looks and sounds like previous one, but them different. This one for compare content after identify the same items (after work of first function).
  • Use Kotlin data classes for compare content of items, it’s very handy.

How call magic from UI class? 🌠

That’s all, folks! 🐷 It was very simple, was not it? Thanks for reading, I hope you enjoy it and this article will be useful! 🙌 Don’t forget press 👏👏👏 if you like this.

Full code of this test project you may find here, also you may install app and check it by yourself. Good luck!

--

--

Android developer. Creator of Scriptum application for manage notes.