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.

Leave a Comment