Skip to content

Instantly share code, notes, and snippets.

@sembozdemir
Created December 8, 2021 21:30
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 sembozdemir/2f44a4c25880f8ad9a2213b101048ca0 to your computer and use it in GitHub Desktop.
Save sembozdemir/2f44a4c25880f8ad9a2213b101048ca0 to your computer and use it in GitHub Desktop.
Kotlin DiffUtil extensions
fun <T> RecyclerView.Adapter<*>.autoNotify(
oldList: List<T>,
newList: List<T>,
compare: (old: T, new: T) -> Boolean
) {
val diff = DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return compare(oldList[oldItemPosition], newList[newItemPosition])
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition] == newList[newItemPosition]
}
override fun getOldListSize() = oldList.size
override fun getNewListSize() = newList.size
})
diff.dispatchUpdatesTo(this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment