Minimize number of layout inflation in RecyclerView Adapter

16 / Dec / 2021 by Ashutosh Bhandari 0 comments

RecyclerView internally uses a ViewPool to save scrap views which are no longer visible on screen and can be recycled. Normally each recyclerview creates a seperate ViewPool which is limited to that recyclerview only. With the help of #RecyclerView.setRecycledViewPool() we can share a ViewPool across multiple RecyclerViews. With this we can reduce the number of layout inflation for RecyclerView.Adapter. As we know layout inflation is an expensive process so this will increase the overall performance.

Few important things to keep in mind when sharing a `ViewPool`.

1. RecycledViewPool is not binded to a Context but since it storing Views it is equivalent to having a Context of the Activity.
Therefore the shared Views should be within the same Activity. We can share `ViewPool` within multiple Activties too but this way it goes out of scope against a Context which might cause Memory Leaks. Hence its recommended to share a View Pool within an Activity only i.e it should be Activity scoped so that it does not live longer than the Activity. Since its activity scoped we will probably having `RecycledViewPool` inside Activity, we can also have it inside `ViewModel` but it’s not a good idea to put it inside `ViewModel`.

2.Shared ViewHolders need to have the same ViewType then only it can be picked from RecycledViewPool.

3. Every RecyclerView.LayoutManager that shares the pool needs to have recycleChildrenOnDetach flag set to true so that views will be available to other RecyclerViews immediately. `recycleChildrenOnDetach` flag is available in official LayoutManagers like LinearLayoutManager and GridLayoutManager.

LinearLayoutManager#setRecycleChildrenOnDetach

Set whether LayoutManager will recycle its children when it is detached from RecyclerView.

4. Setting Pool size – The default limit of viewHolders which could be held by a Pool against a View type is 5. If you want to change it you can do it by calling
`setMaxRecycledViews`. You should change the limit depending on the number of appearance of the View Type on screen.
You can make the pool size bigger for a View Type if it appears many times on screen on other hand
for a View type that is more rare and appears on screen only once you could set the size of the pool to 1 to save memory.

For our example to share a View Pool we create an instance of `RecyclerView.RecycledViewPool` inside Activity.

Now we can share this View Pool instance among multiple RecyclerView in child Fragments or in this Activity too.

That’s all we have to do . Keep this in mind that shared ViewHolders need to have the same View Type,
that’s because ViewHolder is picked from the RecycledViewPool based on the View Type returned from #getItemViewType() .

This is very useful when we have several fragments in Activity showing list of same view types or multiple Recycler Views in same Fragment/Activity . Once you set the shared View Pool you will find out the number of layout inflation from `#onCreateViewHolder`  are decreased by almost 40 % .

FOUND THIS USEFUL? SHARE IT

Tag -

Android

Leave a Reply

Your email address will not be published. Required fields are marked *