I want my RecyclerView to not recycle some items
Use this:
recyclerView.getRecycledViewPool().setMaxRecycledViews(TYPE_CAROUSEL, 0);
This will not recycle any view of viewType TYPE_CAROUSEL
but if the item count of this type is very high, then it will reduce your performance significantly, maybe even cause OOMEs
EDIT
After reading MML13's answer, I think it might work for you. You are worried about items of your carousel being reinflated when that view is rebinded in outer RecyclerView. If all those carousel's are of same type, i.e., they all use same adapter, you can keep the adapter inside outer RecyclerView's ViewHolder, and just swap out data and call notifyDataSetChanged()
or notifyItemRangeChanged(...)
on this adapter when it's rebinded. This will recycle all carousel views and all views inside those carousels.
You can also set below code in your onBindViewholder() method of adapter.
holder.setIsRecyclable(false);
I have taken reference from this link
http://aphoe.com/blog/prevent-androids-recyclerview-recycling-views/
You can add in your adapter:
@Override
public void onBindViewHolder(final CommentViewHolder viewHolder, final int position) {
viewHolder.setIsRecyclable(false);
}
Informs the recycler whether this item can be recycled. Views which are not recyclable will not be reused for other items until setIsRecyclable()
is later set to true. Calls to setIsRecyclable()
should always be paired (one call to setIsRecyclabe(false)
should always be matched with a later call to setIsRecyclable(true)
). Pairs of calls may be nested, as the state is internally reference-counted.