In my RecyclewView's adapter I am using DiffUtils to update only those items which were modified. While doing this I've noticed that ImageViews are flickering on every update and while debugging I found out that onCreateViewHolder
method of my adapter is being called everytime I update the data, so viewholders are not being reused like they should. When I get rid of DiffUtil and use simple notifyDataSetChanged
flickering dissapears.
So why are those ViewHolders being recreated and how can I fix that?
Here is my DiffUtil callback:
class MyDiffUtilCallback(val newList: List<Item>, val oldList: List<Item>) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return newList[newItemPosition].id == oldList[oldItemPosition].id
}
override fun getOldListSize(): Int = oldList.size
override fun getNewListSize(): Int = newList.size
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return newList[newItemPosition] == oldList[oldItemPosition]
}
}
And how I update my adapter:
fun updateItems(items: List<Item>) {
val diff = DiffUtil.calculateDiff(MyDiffUtilCallback(items, this.items))
this.items = items
diff.dispatchUpdatesTo(this)
}
EDIT:
I looked into the issue some more and noticed that viewholders are also being recreated when using notifyDataSetChanged
, I always thought that during update viewholders are being reused but I guess that is not the case.
Anyway image flickering is still visible only when using DiffUtil and not notifyDataSetChanged
and I don't know why