Pau*_*aul 3 android android-recyclerview
RecyclerView 中有一些关于 RecyclerView 的主题,但我发现其中大部分不适合我的情况。我的情况是我有一个 RecyclerView(垂直线性布局管理)显示一个 CardView 列表,每个 Cardview 包含一个内部 RecyclerView(水平线性布局管理)。问题全在于滚动时的性能,根本不流畅。我注意到如果我评论内部 Recyclerview 的 setAdapter,滚动变得平滑,但我使 CardView 没有更新新列表。代码类似于以下内容:
onBindViewHolder...{
holder.innerRecycler.setAdapter(new InnerAdapter((data));
// comment that line make the outer recyclerview smoothly but the CardView data not updated thanks to the view recycling.
}
Run Code Online (Sandbox Code Playgroud)
我知道可滚动视图内的可滚动视图不是一个好主意,但我没有看到任何其他选择。以前有人遇到过这种布局吗?谢谢。
更新(添加更多代码)。
// init outer recyclerview
mOuterRecyclerView = (RecyclerView) findViewById(...);
mOuterRecyclerView.setLayoutManagement(new LinearLayoutManagement(this));
mOuterRecyclerView.setHasFixedSize(true);
mOuterRecyclerView.setAdapter(new OuterAdapter(dataList));
// The adapter class for the outer one
onBindViewHolder(...){
final dataItem = mItems.get(position);
holder.innerRecycler.setAdapter(new InnerAdapter(dataItem.getList()));
}
// the holder for the outer
class MyHolder extends ViewHolder{
RecyclerView innerRecycler;
public MyHolder(View view){
super(..);
innerRecycler = findViewById(...);
}
}
// the adapter for the inner
onBindViewHolder(...){
final dataItem = mItems.get(pos);
// async loading
holder.tvTitle.setText(dataItem.getTitle);
}
Run Code Online (Sandbox Code Playgroud)
布局非常简单,所以我不在这里发布完整的代码。:)
小智 6
@Paul 我有同样的要求,下面的技巧奏效了。将父回收视图放在 NestedScrollView 和 onCreate 方法中,设置。
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)
这可以防止父回收站的滚动并且滚动变得绝对平滑。
只要它们沿不同的轴滚动,这种方法就没有问题。您可以启用RecyclerView.startNestedScroll(int)并处理过度滚动等情况。这种延迟将是因为您每次都重新启动适配器。您可以尝试不同的方法,例如维护适配器映射并在 bindVH 中调用RecyclerView.swapAdapter(args...)。
另一个好的步骤也可以是使用RecyclerView.setRecycledViewPool(args...)为回收视图使用公共池 。我已经创建并使用了 100 多个带有嵌套(不同轴)回收器的项目列表,并且没有遇到问题。
如果您愿意提供更多代码(您编写的地方async loading),我可以为您提供更多帮助。但我建议您阅读 API 和设计模式,这应该可以解决您的问题。
| 归档时间: |
|
| 查看次数: |
6519 次 |
| 最近记录: |