Optimized nested RecyclerView
RecyclerView is a more advanced version of ListView that reuses the same view to prevent additional views from being created to provide a smooth scrolling experience. RecyclerView does this by keeping a pool of views that are no longer visible and can be recycled.
Sometimes we need to nest RecyclerView to create some layouts. Consider the situation of horizontal RecyclerView inside the vertical RecyclerView.
In the figure above you can see a vertical RecyclerView that can be rolled horizontally by placing a RecyclerView in another RecyclerView.
Internal RecyclerView recycles the view and gives you smooth scrolling as the user slides sideways. However, this is not the case when the user scrolls vertically. Every view of the internal RecyclerView is inflate again, because each nested RecyclerView has its own view pool.
We can solve this problem by setting up a single view pool for all internal RecyclerViews.
Copy the code
RecyclerVIew allows you to create custom view pools for RecyclerVIew. The code looks like this:
public 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);
}Copy the code
So now all internal RecyclerViews have the same pool of views that can use each other’s views. This results in fewer views being created and better scrolling performance.
The original link