Demystifying DiffUtil.ItemCallback Class

Jermaine Dilao
3 min readJan 25, 2021

--

It’s more than just comparing item ids.

Photo by Dayne Topkin on Unsplash

Often, I see some new developers who are using DiffUtil.ItemCallback class the wrong way. Like this:

fun areItemsTheSame(oldItem: Item, newItem: Item): Boolean {
return oldItem.id == newItem.id
}
fun areContentsTheSame(oldItem: Item, newItem: Item): Boolean {
return oldItem.id == newItem.id
}

Surely, this will work just fine if you display your list once and NEVER update it again.

But, what if you start updating the list? Let’s say you have a refresh function. This is when the wrong implementation hits you.

You will be stuck on calling adapter.submitList(newList) and seeing your list not updating the items that should have changed.

Before we dive in on how to fix it. Let’s first explore what those 2 methods mean.

https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil.ItemCallback#public-methods
https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil.ItemCallback#public-methods
  • areItemsTheSame() — this method will be called to check whether old and new items are the same.
  • areContentsTheSame() —this method will be called to check whether old and new items represent the same item visually. This will only be called when areItemsTheSame() returns true.

Let’s get to the real deal

When you try to update your list by calling adapter.submitList(). This will first call areItemsTheSame() to check whether two items are the same. This is pretty straightforward from here.

Want to read this story later? Save it in Journal.

The real issue here plays with areContentsTheSame() . The fields that you should check here are the fields that can be visually seen on the list. If your list displays title and description . Then this method should contain these:

fun areContentsTheSame(oldItem: Item, newItem: Item) {
return oldItem.title == newItem.title &&
oldItem.description == newItem.description
}

This will then instruct the adapter to say “Hey, this item contains different contents now, please update this one with the new contents in the UI.”

As your item UI in the list grows (i.e. adding an avatar, username, address, etc.), you should update areContentsTheSame() accordingly so that when you update your list, it will be able to identify which items are stale in the UI.

That’s basically it for this article!

With this implementation, calling adapter.submitList() should work properly now.

I hope this article was able to enlighten someone on how to use this properly.

Til next time. Happy coding!

PS: If you’re still using adapter.notifyDataSetChanged() in updating your list. Please avoid this, as this will perform an expensive operation in updating the entire list including those unnecessary ones. I would strongly recommend using ListAdapter + proper DiffUtil.ItemCallback implementation.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jermaine Dilao
Jermaine Dilao

Written by Jermaine Dilao

A Work in Progress Android Developer. You can check me @ https://jermainedilao.framer.website. | 💻 Senior Android Engineer @ Speakap

Responses (2)

Write a response