我正在使用嵌套的RecyclerView.在垂直RecyclerView里面的意思我有多个水平回收视图
我将适配器附加到父RecyclerView的onBindViewHolder方法内的水平recylerviews,如下所示.
@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,position)); break; //few more types like this } }
现在每当我滚动页面时它都会滞后一点,因为我在OnBindViewHolder上将适配器连接到水平RecyclerView
并且可以有N个TYPE_PRODUCT或任何类型的水平列表.
意味着可以有更多相同类型的水平列表.
知道如何优化这个东西并提高滚动速度.
它是滞后的,因为之前每次为列表调用setAdapter.
更新我正在扩展LinearLayoutManager,并且我正在设置extraLayout空间,这已经修复了我的问题,但我不知道这是正确的方式我是设置额外的空间如下.
layoutManager.setExtraLayoutSpace(2 * this.getResources().getDisplayMetrics().heightPixels);
而follwoing是自定义布局管理器类
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; }
}