【问题标题】:RecyclerView is laggyRecyclerView 滞后
【发布时间】:2017-07-15 07:06:40
【问题描述】:

我的 RecyclerView 中有 8 个项目,其中包含一个图像和一个标题。 性能非常滞后。

我使用毕加索加载图像。图像甚至没有高分辨率导致延迟。

这是我的适配器:

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder> {

private Context context;
private List<Category> categories;

class CategoryViewHolder extends RecyclerView.ViewHolder {
    TextView categoryTitle;
    ImageView categoryImage;

    CategoryViewHolder(View view) {
        super(view);
        categoryTitle = (TextView) view.findViewById(R.id.category_title);
        categoryImage = (ImageView) view.findViewById(R.id.category_thumbnail);
    }
}

public CategoryAdapter(Context context, List<Category> categories) {
    this.context = context;
    this.categories = categories;
}

@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item, parent, false);
    return new CategoryAdapter.CategoryViewHolder(itemView);
}

@Override
public void onBindViewHolder(final CategoryViewHolder holder, int position) {
    Category category = categories.get(position);
    holder.categoryTitle.setText(category.getCategoryTitle());
    Picasso.with(context)
            .load(category.getCategoryImage())
            .into(holder.categoryImage);
}

@Override
public int getItemCount() {
    return categories.size();
}
}

【问题讨论】:

  • 使用方法跟踪来确定您将时间花在哪里。
  • 是什么让你认为这是回收者的观点?延迟行为不能与获取图像可能需要大量时间的事实相关联吗?如果你注释掉毕加索的电话,它仍然滞后吗?
  • 滚动不能滞后——毕加索是异步的。你在别处做错了什么
  • @Fred 我没有通过网络获取图像。它们存储在本地。
  • 那我猜问题出在其他地方……要么设备太弱,要么代码中存在其他问题。

标签: android picasso android-recyclerview


【解决方案1】:

您是否为您的活动设置了任何背景图片。如果是,则检查背景图像大小。可能是太大了。这会让它变得迟钝。

【讨论】:

  • 没有背景图片。
【解决方案2】:

一种方法是在从毕加索获取图像时异步调用该方法。请查看更多信息中的以下链接How to asynchronously call a method in Java

【讨论】:

    【解决方案3】:

    我面临同样的问题,这对我有用,在初始化 recyclerview 后添加以下行

    recyclerView.setNestedScrollingEnabled(false);
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2019-05-08
      • 2018-01-30
      • 1970-01-01
      • 2014-10-08
      • 2017-06-14
      • 2021-08-14
      • 1970-01-01
      相关资源
      最近更新 更多