Skip to content

Instantly share code, notes, and snippets.

@k0siara
Created May 30, 2021 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0siara/765963f821c0bac7fc5bfd2eb123c402 to your computer and use it in GitHub Desktop.
Save k0siara/765963f821c0bac7fc5bfd2eb123c402 to your computer and use it in GitHub Desktop.
DiffUtilCallback.kt
class DiffUtilCallback(
private val oldItems: List,
private val newItems: List
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldItems[oldItemPosition].id == newItems[newItemPosition].id
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
// It works properly if Item is a data class
// Otherwise, we should check if all fields of the items are the same
return oldItems[oldItemPosition] == newItems[newItemPosition]
}
override fun getOldListSize() = oldItems.size
override fun getNewListSize() = newItems.size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment