Last active
December 27, 2018 12:19
-
-
Save AdiD253/f636e3f50ca83cd1f8a382e435b7048b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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