RecyclerView.ViewHolder's setIsRecyclable() func

0

I want to set my view holder to not recycle, here's the code:

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
    viewHolder.setIsRecyclable(false);
}

However, when I see the documentation here: https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder#setIsRecyclable(boolean), it is written that the setIsRecycleable() should always be paired and I have no idea about this. Can anybody provide a sample code for this case? When should I call the setIsRecyclable(true) again? Thanks.

enter image description here

3
0

Not recycling the ViewHolder just means that specific ViewHolder will be retained and not overwritten when there is new data to bind, the problem with that is the Adapter will then need to supply another ViewHolder to make up for the one it can't reuse.

That is why you need to eventually let it recycle i.e. setIsRecycleable(true) because it kinda defeats the point of the RecyclerView if it ends up having to create new views to represent data.

A reason you might want to turn off the recycle is to avoid interruptions, maybe the ViewHolder is playing an animation, or loading a video. Once its done you could then turn on the recycle, to release the ViewHolder to make sure it can be used again.

2021-11-24 06:12:08
0

It's better to use Listview in this case. The whole meaning of recyclerview is to recycle the view. Else see this post https://stackoverflow.com/a/36275862/3094367

2021-11-24 06:03:40
0

Actually the recycle is related to viewType.
You just change every itemType is unique
like:

@Override
public int getItemType(int position){
    return position
}
2021-11-24 06:28:44

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................