I am currently playing with the hasStableIds
of recycler view and I have the following observations:
1) When the hasStableIds = false
:
notifyDataSetChanged()
called, it will make all views as dirty and recycle them again, due to which onCreateViewHolder()
and onBindViewHolder()
will be called for each item.2) When the hasStableIds = true
:
notifyDataSetChanged()
called, it will check for the ids and then decide whether to create a viewHolder for the recyclerView item. If the data set is not changed then it will only call onBindViewHolder()
not onCreateViewHolder()
again.So as per the above behaviour, I am confused that whether we should always use our recyclerview with the hasStableIds = true
so that it will avoid creating viewholders again? or is there any specific conditions in which we should use it?
According to docs
Enables adapter publishes a unique Id/value that can act as a key for the item at a given position in recyclerView.
If that item is relocated in the data set, the ID returned for that item should be the same. this enables you to avoid sudden blink when adapter notify changes or perform any positional action,
caution!, it's not advisable unless you know that your lists items/content will not change during updates.
Android hasStableIds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With