16

I am trying to understand what the setRecycledViewPool method actually does along with the RecyclerView in the following line of code where mrecyclerView is a RecyclerView object:

mrecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());

I read the Android documentation link and I still don't understand what it does clearly. Can someone explain to me its use and when to use it?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Abhilash Reddy
  • 428
  • 1
  • 4
  • 19

4 Answers4

23

setRecycledViewPool(...) can be useful when we have a nested RecyclerView. See this blog post for details. A short description of the same link is added here.

Consider a case where you have a nested RecyclerViews and inner RecycleViews share the same view structure. RecycledViewPool provides a seemless way to share views between these inner (nested) RecyclerViews.

An example of such case could be seen in the following image:

enter image description here

As you can see the types of views for both lists are same.

Farid
  • 2,317
  • 1
  • 20
  • 34
Aliaksei
  • 3,542
  • 1
  • 16
  • 21
  • 4
    Also we should be careful with listeners for clicking items. Since if we use several RecyclerViews our items can have equal positions, so click events can overlap. – Aliaksei Nov 09 '19 at 17:40
  • 1
    so what do you suggest to avoid this? create a separated adapter for each recycler or use the same? – Moustafa EL-Saghier Feb 14 '22 at 15:18
19

From docs:

Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views. This can be useful if you have multiple RecyclerViews with adapters that use the same view types, for example if you have several data sets with the same kinds of item views displayed by a ViewPager.

By default, 5 ViewHolders are retained in the pool for a particular viewType. If you want to change that count, it may be done this way:

recyclerView.getRecycledViewPool()
            .setMaxRecycledViews(SOME_VIEW_TYPE, POOL_CAPACITY);

From this blog post:

So how do we choose the optimal size of the pool? It seems that the optimal strategy is to extend the pool right before you’ll need it to be big, and shrink it right afterwards. One dirty way to implement this is the following:

recyclerView.getRecycledViewPool().setMaxRecycledViews(0, 20);
adapter.notifyDataSetChanged();
new Handler().post(new Runnable() {
    @Override
    public void run() {
        recyclerView.getRecycledViewPool()
                    .setMaxRecycledViews(0, 1);
    }
});
azizbekian
  • 60,783
  • 13
  • 169
  • 249
3

When the RecyclerView is expected to show more than 5 views on the screen at the same time (5 is the current default), it's recommended to enlarge it to what you think will be the max. This will allow to do an actual recycling (the purpose of RecyclerView...) when scrolling, to avoid re-creation of Views.

In fact, because I actually think that it's almost never known how many will be be shown on the screen (different screens resolutions, etc...) , I think it's best to just set it to be the max :

fun RecyclerView.setMaxViewPoolSize(maxViewTypeId: Int, maxPoolSize: Int) {
    for (i in 0..maxViewTypeId)
        recycledViewPool.setMaxRecycledViews(i, maxPoolSize)
}

Usage:

    recyclerView.setMaxViewPoolSize(MAX_TYPE_FOR_THE_ADAPTER_YOU_MADE, Int.MAX_VALUE)

I personally don't understand why it's not the default behavior. The whole point of using a RecyclerView is to recycle the Views. When scrolling, it should, by default, recycle views that were just used.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Life saver! We had an issue where after a certain number of elements (well, 5) `onCreateViewHolder` would be called repeatedly. This post answered many questions at once. Thank you. – Beko Mar 12 '21 at 11:22
  • @Beko I personally thought this issue is gone now, that nobody should use it anymore, and that maybe I was wrong. How odd. Can you please research it further? Maybe I'm wrong with this. – android developer Mar 12 '21 at 14:52
  • Hey, sorry, I forgot to reply. Why did you think that this wasn't an issue anymore? Anyway, I'll probably have to take a deeper look into it at some point. I've had a bit of trouble with RecyclerViews lately. – Beko Mar 17 '21 at 09:42
  • 1
    @Beko I don't know. I thought that by now it should not be needed anymore. Please check it out and let me know. – android developer Mar 17 '21 at 09:56
2

I haven't used it myself, but from what I can understand reading the docs its a way to use views you recycle in one RecyclerView with another.

So if you have a RecyclerView with a bunch of decked out CardViews, and you would like to recycle those same views for another RecyclerView you could pass it a shared RecycledViewPool. Now both RecyclerView will take from the shared view pool.

DejanRistic
  • 2,039
  • 18
  • 13
  • Damn.. that sounds so interesting ... I would definitely like to know more about it , can you explain it more with some example and implementation. – Abhilash Reddy Apr 11 '17 at 03:39