I usually use diffutil
with Recyclerview
when needed. Right now I have situation where the items I get from backend look like this:
data class CarouselItem(var url: String, var pictureUrl: String, var visible: String)
All 3 fields can be the same for 2 items. I am thinking what is the best way to create diffUtil here.
IMHO I should make some wrapper that will add a field by which I can differentiate 2 items (for example ID field).
Also in this case in my diffUtil
, override function areContentsTheSame
would have to compare all 3 fields of item like this:
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
return oldList[oldItemPosition].visible == newList[newItemPosition].visible && oldList[oldItemPosition].pictureUrl == newList[newItemPosition].pictureUrl &&
oldList[oldItemPosition].url == newList[newItemPosition].url
}
I think this takes processor power almost the same as when I don't use DiffUtil
since it compares the whole item to the whole item (no benefit of comparing only 1 field).
Is this the right way to do it and is it overkill in this case? I would like to know what are my best option as an android developer when I have situations like this (items like this) in the future? (I like to follow clean code concepts and do things the best way if possible)