Skip to content

Instantly share code, notes, and snippets.

@AdiD253
Last active December 27, 2018 12:19
Show Gist options
  • Save AdiD253/f636e3f50ca83cd1f8a382e435b7048b to your computer and use it in GitHub Desktop.
Save AdiD253/f636e3f50ca83cd1f8a382e435b7048b to your computer and use it in GitHub Desktop.
fun <T : DiffItem, R : RecyclerView.ViewHolder> RecyclerView.Adapter<R>.autoNotify(oldList: List<T>, newList: List<T>) {
val diffItemCallback = object : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
oldList[oldItemPosition].getItemId() == newList[newItemPosition].getItemId()
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
oldList[oldItemPosition].getDiff() == newList[newItemPosition].getDiff()
override fun getOldListSize() = oldList.size
override fun getNewListSize() = newList.size
}
DiffUtil.calculateDiff(diffItemCallback).dispatchUpdatesTo(this)
}
interface DiffItem {
fun getItemId(): String
fun getDiff(): String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment