Created
December 8, 2021 21:30
-
-
Save sembozdemir/2f44a4c25880f8ad9a2213b101048ca0 to your computer and use it in GitHub Desktop.
Kotlin DiffUtil extensions
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> 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