Last active
December 26, 2020 09:58
-
-
Save AdiD253/faf401d3b6f23d3ecd6280da8a22d06f 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
class SampleAdapter : RecyclerView.Adapter<SampleViewHolder>() { | |
private var itemList: List<SampleItem> = listOf() | |
fun setItems(newList: List<SampleItem>) { | |
val diffItemCallback = object : DiffUtil.Callback() { | |
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) = | |
this.itemList[oldItemPosition].getItemId() == newList[newItemPosition].getItemId() | |
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) = | |
this.itemList[oldItemPosition].getDiff() == newList[newItemPosition].getDiff() | |
override fun getOldListSize() = itemList.size | |
override fun getNewListSize() = newList.size | |
} | |
val diffResult = DiffUtil.calculateDiff(diffItemCallback) | |
diffResult.dispatchUpdatesTo(this@SampleAdapter) | |
} | |
(...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment