I am using a nested RecyclerView. The tool inside the vertical RecyclerView I have several types of horizontal recycler
I connect the adapter to horizontal recylerviews inside the onBindViewHolder method of the parent RecyclerView as follows.
@Override public void onBindViewHolder(final MainViewHolder holder, final int position) { switch (holder.getItemViewType()) { case TYPE_PRODUCT: ((ListHolderProduct) holder).itemTitle.setText(browseCategoryHomePageItems.get(position).displayName.toUpperCase()); ((ListHolderProduct) holder).recyclerView.setAdapter(new CarouselProductsRecyclerAdapter(context , browseCategoryHomePageItems.get(position).products , R.layout.activity_categoryhome_products_grid_item , nestedRecyclerItemClickedListener , position)); break; case TYPE_DEAL: ((ListHolderDeal) holder).itemTitle.setText(browseCategoryHomePageItems.get(position).displayName.toUpperCase()); ((ListHolderDeal) holder).recyclerView.setAdapter(new CarouselDealsRecyclerAdapter(context , browseCategoryHomePageItems.get(position).dealItems , R.layout.activity_categoryhome_deals_grid_item , nestedRecyclerItemClickedListener , position)); break;
Now, when I scroll the page, it lags a little, since I connect the adapter to the horizontal RecyclerView on the OnBindViewHolder
And there may be N Number of TYPE_PRODUCT or any types of horizontal lists. This means that there can be more than one horizontal list of the same type.
Any idea how I can optimize this thing and improve scroll speed.
This is a lag since the setAdapter is called every time for the list earlier.
Update on this issue I am expanding the LinearLayoutManager, and in this I am setting the extraLayout space, which fixed my problem, but I do not know if this is the right way or not. I set the extra space as shown below.
layoutManager.setExtraLayoutSpace(2 * this.getResources().getDisplayMetrics().heightPixels);
and then the custom layout manager class
public class PreCachingLayoutManager extends LinearLayoutManager { private static final int DEFAULT_EXTRA_LAYOUT_SPACE = 600; private int extraLayoutSpace = -1; private Context context; public PreCachingLayoutManager(Context context) { super(context); this.context = context; } public PreCachingLayoutManager(Context context, int extraLayoutSpace) { super(context); this.context = context; this.extraLayoutSpace = extraLayoutSpace; } public PreCachingLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); this.context = context; } public void setExtraLayoutSpace(int extraLayoutSpace) { this.extraLayoutSpace = extraLayoutSpace; } @Override protected int getExtraLayoutSpace(RecyclerView.State state) { if (extraLayoutSpace > 0) { return extraLayoutSpace; } return DEFAULT_EXTRA_LAYOUT_SPACE; }
}