android – 嵌套的recylerview滞后,而前几个滚动,然后顺利滚动?

我正在使用嵌套的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;
}

}

解决方法

不要每次在onBindViewHolder上创建水平适配器,而是在每个ViewHolder类(ListHolderDeal,ListHolderProduct)中创建适当的适配器.然后在垂直Recyclerview的onBindViewHolder上只替换该适配器的数据,如果水平的RecyclerView没有适配器,则使用视图适配器设置它.如果它是剂量,则替换该适配器的数据集并调用notifyDataSetChange.使用这种方法,您可以隐式使用适配器池,因此GC可能不会打扰您.

我希望它有所帮助.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...