Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use hasStableIds true for the recyclerView?

I am currently playing with the hasStableIds of recycler view and I have the following observations:

1) When the hasStableIds = false:

  • Once the 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:

  • Once the 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?

like image 656
Shubham Hupare Avatar asked Nov 07 '22 16:11

Shubham Hupare


1 Answers

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

like image 195
Chetan Gupta Avatar answered Dec 16 '22 07:12

Chetan Gupta