简体   繁体   English

如何使RecyclerView在NestedScrollView中进行回收?

[英]How to make RecyclerView do recycling inside NestedScrollView?

I tried to put several views, including RecyclerView , into a NestedScrollView . 我试图将几个视图(包括RecyclerView )放入NestedScrollView I used setNestedScrollingEnabled(false) and it looked nice for small data sets but started to be laggy for bigger ones. 我使用了setNestedScrollingEnabled(false) ,它看起来很适合小数据集,但对于较大的数据集开始变得迟钝。

After spending some time on logging the onCreateViewHolder() method I understood that the recycler view creates them all at once as the old ListView . 在花了一些时间记录onCreateViewHolder()方法之后,我明白了recycleler视图会一次创建它们作为旧的ListView I tried to find reasons of such behavior in RecyclerView docs, but I found it in ScrollView description : 我试图在RecyclerView文档中找到这种行为的原因,但我在ScrollView 描述中找到了它:

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. 您永远不应该使用带有ListView的ScrollView,因为ListView负责自己的垂直滚动。 Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView. 最重要的是,这样做会使ListView中的所有重要优化都无法处理大型列表,因为它有效地强制ListView显示其整个项目列表以填充ScrollView提供的无限容器。

I hoped that NestedScrollView would solve the issue but it appears that it hadn't. 我希望NestedScrollView可以解决这个问题,但似乎没有。

Are there any ways, for example, using some custom LayoutManager to use RecyclerView with its optimizations of view recycling? 有没有什么方法,例如,使用一些自定义LayoutManager来使用RecyclerView优化视图回收?

Ps Of course, I know about method getItemViewType(int pos) and possibility to add custom headers and footers using this technique but it looks like an ugly workaround for me. Ps当然,我知道方法getItemViewType(int pos)以及使用这种技术添加自定义页眉和页脚的可能性,但对我来说它看起来像一个丑陋的解决方法。 And yes, I am using it at the moment, because it is better to have code that is harder to maintain than such big performance issue. 是的,我现在正在使用它,因为拥有比这么大的性能问题更难维护的代码更好。

You need to change your layout like below, You are using the NestedScrollView than u need to add the android:nestedScrollingEnabled property inside the RecyclerView 您需要更改您的布局,如下所示,您使用的是NestedScrollView不是需要在RecyclerView添加android:nestedScrollingEnabled属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/YOUR_NEESTED_SCROLLVIEW"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/YOUR_RECYCLVIEW"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:nestedScrollingEnabled="false" />

    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

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

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