Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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