简体   繁体   English

RecyclerView里面的RecyclerView并不顺利

[英]RecyclerView inside RecyclerView not smoothly

There are some topics about RecyclerView inside RecyclerView but I see most of them do not fit my case. 在RecyclerView中有一些关于RecyclerView的主题,但我发现它们中的大多数都不适合我的情况。 My case is I have a RecyclerView (verticle linear layout management) displays a list of CardView, each Cardview contains a inner RecyclerView (horizontal linear layout management). 我的情况是我有一个RecyclerView(垂直线性布局管理)显示一个CardView列表,每个Cardview包含一个内部的RecyclerView(水平线性布局管理)。 The problem is all about performance when scrolling, it is not smooth at all. 问题在于滚动时的性能,它根本不是平滑的。 I notice if I comment the setAdapter for the Inner Recyclerview, the scrooling become smooth, but I make the CardView not updated the new list. 我注意到如果我为内部Recyclerview评论setAdapter,scrooling变得平滑,但我让CardView没有更新新列表。 The code is something similar to this: 代码与此类似:

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.
}

I know a scrollable view inside a scrollable view is not a good idea but I dont see any other choices. 我知道可滚动视图中的可滚动视图不是一个好主意,但我没有看到任何其他选择。 Anyone face to this kind of layout before?. 以前有人面对这种布局吗? Thanks. 谢谢。

UPDATE (add more code). 更新(添加更多代码)。

// 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);
}

The layout is pretty simple so I dont post the fully code here. 布局非常简单,所以我不在这里发布完整的代码。 :) :)

@Paul i had the same requirement and the below trick worked. @Paul我有同样的要求,下面的技巧奏效了。 Put the parent recyclerview inside NestedScrollView and in your onCreate method, set. 将父recyclelerview放在NestedScrollView和onCreate方法中,设置。

mRecyclerView.setHasFixedSize(true);
mRecyclerView.setNestedScrollingEnabled(false);

This prevents scroll of parent recyclerview and scroll becomes absolutely smooth. 这可以防止父回收视图的滚动和滚动变得绝对平滑。

There is no issue in such an approach as long as they scroll along different axes.You could enable RecyclerView.startNestedScroll(int) and also handle situations like overscroll.This delay would be because you are reinitiating an adpater everytime. 这种方法没有问题,只要它们沿着不同的轴滚动。您可以启用RecyclerView.startNestedScroll(int)并处理过度滚动等情况。这种延迟是因为您每次都重新启动adpater。 You could try something different like maintaining a map of adpaters and calling RecyclerView.swapAdapter(args...) in bindVH. 您可以尝试不同的方法,例如维护adpaters地图并在bindVH中调用RecyclerView.swapAdapter(args ...)

Another good step could also be using a common pool for recycled views using RecyclerView.setRecycledViewPool(args...) . 另一个好的步骤也可以是使用RecyclerView.setRecycledViewPool(args ...)将公共池用于回收视图。 I have created and used a list of 100+ plus items with a nested (different axis) recycler and have not faced issues . 我创建并使用了一个包含嵌套(不同轴)回收器的100多个项目的列表,并且没有遇到问题。

If you would provide more code( where you have written async loading ) I could help you more.But I suggest you go through the API and the design patterns and that should solve your issue. 如果您要提供更多代码(您编写async loading ),我可以为您提供更多帮助。但我建议您浏览API和设计模式,这应该可以解决您的问题。

Since there are some user ask me how to archive, I'm going to post what I do. 由于有一些用户问我如何存档,我将发布我的工作。 The sugession that @Droidekas seems a good point but I dont follow that. @Droidekas似乎是一个好点,但我不遵循这一点。

What I did is: 我做的是:

  1. Each horizontal recycler view is a item of a vertical recycler view 每个水平回收者视图都是垂直回收者视图的项目

  2. When I need to set the data for the particular vertical item (onBindViewHolder), I use the horizontal recycler adapter setNotifyDataSetChanged instead of setting a completely new adapter or swapping it. 当我需要为特定的垂直项目(onBindViewHolder)设置数据时,我使用水平回收器适配器setNotifyDataSetChanged而不是设置一个全新的适配器或交换它。 This way, I got the vertical recyler run very smoothly. 通过这种方式,我使垂直再生器运行非常顺畅。

The vertical recyler adapter structure could be: 垂直再循环适配器结构可以是:

Vertical Item view holder  -> Vertical Item[Name, List<Horizontal item>,...]
Vertical Item view holder  -> Vertical Item[Name, List<Horizontal item>,...]

In OnBindViewHolder 在OnBindViewHolder中

holder.setData(VerticalItem)

In the Holder, I have a setData method looks like: 在Holder中,我有一个setData方法,如下所示:

setData(VerticalItem item){
    mItems = item.getHorizontalItems();
    mHorizontalAdapter.notifyDataSetChanged();
}

Hope it helps. 希望能帮助到你。 :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM