如何使RecyclerView在NestedScrollView中进行回收? [英] How to make RecyclerView do recycling inside NestedScrollView?

查看:459
本文介绍了如何使RecyclerView在NestedScrollView中进行回收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将包括RecyclerView在内的多个视图放入NestedScrollView中.我使用了setNestedScrollingEnabled(false),它对于小型数据集看起来不错,但对于大型数据集却开始变得迟钝了.

I tried to put several views, including RecyclerView, into a NestedScrollView. I used setNestedScrollingEnabled(false) and it looked nice for small data sets but started to be laggy for bigger ones.

花了一些时间记录onCreateViewHolder()方法后,我了解到回收者视图会像旧的ListView一样立即创建它们.我试图在RecyclerView文档中找到这种行为的原因,但在ScrollView

After spending some time on logging the onCreateViewHolder() method I understood that the recycler view creates them all at once as the old ListView. I tried to find reasons of such behavior in RecyclerView docs, but I found it in ScrollView description:

永远不要将ScrollView与ListView一起使用,因为ListView 照顾自己的垂直滚动.最重要的是,这样做 击败了ListView中用于处理的所有重要优化 具有大列表,因为它有效地迫使ListView显示 它的整个项目列表,以填充由...提供的无限容器 滚动查看.

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. 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.

我希望NestedScrollView能解决该问题,但事实并非如此.

I hoped that NestedScrollView would solve the issue but it appears that it hadn't.

有什么方法,例如,使用一些自定义的LayoutManager来使用RecyclerView及其优化的视图回收功能?

Are there any ways, for example, using some custom LayoutManager to use RecyclerView with its optimizations of view recycling?

P.s.当然,我知道方法getItemViewType(int pos)以及使用此技术添加自定义页眉和页脚的可能性,但对我来说这似乎是一个丑陋的解决方法.是的,我目前正在使用它,因为拥有比这样大的性能问题更难维护的代码会更好.

P.s. 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. 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.

推荐答案

您需要像下面那样更改布局,您正在使用NestedScrollView而不是您需要在RecyclerView内添加android:nestedScrollingEnabled属性

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

<?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>

这篇关于如何使RecyclerView在NestedScrollView中进行回收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆