Interview frequency: …
Short answer
DiffUtil computes the differences between two lists on an asynchronous background and applies precise, positional animations only for the changed items instead of redrawing the entire list.
Explanation
Calling notifyDataSetChanged rebuilds and updates every visible item on the screen and cancels scroll animations. DiffUtil computes the smallest sequence of edit operations (add, remove, move, partial change) using Eugene W. Myers' algorithm. ListAdapter wraps this logic in a ready-made class that automatically sends new lists through submitList on a background thread.
Code example
class UserDiffCallback : DiffUtil.ItemCallback<User>() {
override fun areItemsTheSame(oldItem: User, newItem: User): Boolean = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: User, newItem: User): Boolean = oldItem == newItem
}
class UserListAdapter : ListAdapter<User, UserViewHolder>(UserDiffCallback()) { ... }
// Smooth update:
userListAdapter.submitList(newList)Common mistakes
- Passing the same mutable reference (MutableList) to submitList without creating a new list, which prevents DiffUtil from detecting any change.
- Writing wrong conditions inside areItemsTheSame that make an ID match completely different items.