A RecyclerView
is more advanced version of ListView which reuses the same view to
prevent additional creation of views to provide a smooth scrolling
experience. The RecyclerView achieves this by keeping a View pool that
holds the views that are no longer visible and can be recycled.
Sometimes
we need to nest RecyclerViews to create some layouts. Consider the case
where you have a horizontal RecyclerView inside a Vertical
RecyclerView.

In
the Image you can see vertical scroling list of horizontal scroling
lists.This is achieved by placing a recyclerView inside another
recyclerView.
When
the user swipes the side-wise the inner RecyclerView recycles the views
and gives you a smooth scroll. But this is not the case when the user
scrolls vertically. Each of the views of the inner RecyclerView is
inflated again. This is because each of the the nested RecyclerViews has
a view pool of its own.
We can fix this by setting a single view pool for all the inner RecyclerViews.
RecyclerView.setRecycledViewPool(RecycledViewPool) allows you to set a custom view pool to your recyclerView. The code looks like thispublic OuterRecyclerViewAdapter(List<Item> items) {
//Constructor stuff
viewPool = new RecyclerView.RecycledViewPool();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create viewHolder etc
holder.innerRecyclerView.setRecycledViewPool(viewPool);
}
So
now as all the inner RecyclerViews have the same view pool, it can use
each other’s scraped views. Which gives much lesser view creation and
better scroll performance.
Comments
Post a Comment