In the previous post we have looked at how to clean up resources in RecyclerView.ViewHolder which makes sure we don’t waste memory unnecessarily. But that’s not all of the things you can do to make RecyclerView scrolling more smooth, there are number of things we can still improve.
In this post we will specifically look at optimizations related to RecyclerView.RecycledViewPool.
So first a bit of theory. Essentially whenever a downward scroll occurs inside RecyclerView the views that disappear on the top are recycled into the RecycledViewPool and then reused for the views appearing on the bottom.
Below are a couple of RecycledViewPool tricks you can use to optimize RecyclerView.
Reusing RecycledViewPool with many RecyclerViews
Lets imagine we have a couple of RecyclerViews on the screen displaying the same type of the ViewHolder such as horizontally displayed apps on Google Play.
By default both visible RecyclerViews will have their own pool which means they are not sharing views. This leads to many views being unnecessarily being inflated and possibly resulting in laggy behaviour of the horizontal scroll.
In order to avoid that we care share the pool using RecyclerView#setRecycledViewPool method.
There are couple of additional things that we will need to keep in mind
shared ViewHolders need to have the same ViewType
This is because ViewHolder is picked from the RecycledViewPool based on the ViewType returned from RecyclerView.Adapter#getItemViewType
every RecyclerView.LayoutManager that shares the pool needs to have recycleChildrenOnDetach flag set to true
As we can read in official documentation
If you are using a RecyclerView.RecycledViewPool, it might be a good idea to set this flag to true so that views will be available to other RecyclerViews immediately.
Setting the optimal RecycledViewPool size
By default RecycledViewPool can hold 5 ViewHolders of the same ViewType.
Since you might have more items of the same ViewType on the screen it might make sense to increase that number. For a ViewHolder that is more rare and appears on the screen only once you could set the size of the pool to 1. Otherwise, sooner or later, the pool will be filled with 5 of those rare ViewHolders, 4 of which will just sit unused, wasting precious memory.
We can set the pool size in the following way
That’s all! You can finally enjoy the smooth scrolling experience in your app!
In this article we have learned how to optimize RecyclerView using RecyclerView.RecycledViewPool!