How to make RecyclerView do recycling inside NestedScrollView?

蹲街弑〆低调 提交于 2019-12-07 02:40:52

问题


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.

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:

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.

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

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

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.


回答1:


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>


来源:https://stackoverflow.com/questions/42782786/how-to-make-recyclerview-do-recycling-inside-nestedscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!