Skip to content

Instantly share code, notes, and snippets.

@justtwago
Last active April 14, 2020 11:17
Show Gist options
  • Save justtwago/8af65eb258d038bbf430e6103d340f89 to your computer and use it in GitHub Desktop.
Save justtwago/8af65eb258d038bbf430e6103d340f89 to your computer and use it in GitHub Desktop.
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