Having RecyclerView inside a NestedScrollView calls onBindView for all the items

后端 未结 4 1620

I have two RecyclerViews placed vertically in a LinearLayout. I need to make both of them scrollable and that is why I have put the LinearLay

相关标签:
4条回答
  • 2021-01-03 21:12

    Add this to nested scroll view android:fillViewport="false"

    0 讨论(0)
  • 2021-01-03 21:14

    Starting from RecyclerView:1.2.0-alpha02 we can use MergeAdapter to solve this problem https://developer.android.com/reference/androidx/recyclerview/widget/MergeAdapter

    0 讨论(0)
  • 2021-01-03 21:22

    I tried your problem by adding 20 items in each recyclerview, with NestedScrollView application called onBindViewHolder method 40 times. As you disabling nested scrolling in Java code i suggest to use Scrollview. By using ScrollView application called onBindViewHolder 33 times.


    If you fix your recyclerView's height to specific size instead of "match-parent" it will reduce call to onBindViewHolder greatly.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="false">
    
    <android.support.v7.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.vishal.my2.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/featured_list"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/all_topic_list"
        android:layout_width="match_parent"
        android:layout_height="300dp" />
    </android.support.v7.widget.LinearLayoutCompat>
    </ScrollView>
    

    If Specifying hardcoded value to recyclerView's height does not meet your application requirement then you can try using ListView instead of recyclerView. pardon me if i am wrong, This was my first time answering any question.

    0 讨论(0)
  • 2021-01-03 21:31

    I had exactly the same problem. RecyclerViews are not meant to be placed inside scroll containers with the same scroll direction. The view recycling only works when the height is set to MATCH_PARENT.

    Depending on the complexity of the content inside of the NestedScrollView and the anticipated amount of RecyclerView items:

    1. Ignore the problem. If there are only a few simple items, you may not need view recycling at all.

      When I hit the problem, I analysed the layouts of other popular apps: For example, WhatsApp only uses RecyclerViews (or ListViews with view recycling) in some parts of their app.

      Particularly, this group settings screen with hundreds of possible items is made of multiple ListViews wrapped by a ScrollView, without any view recycling.

    2. Replace the NestedScrollView with a single ReyclerView with multiple item types and put all of your scrollable content inside of it. This is the way to go if you need view recycling.

      Beware that you also have to convert all the other content in the NestedScrollView (headers and footers, spacing) to RecyclerView items with their own ViewHolders.

      If the setup is rather simple, I would recommend you to implement it without additional libraries, following the link above.


    There are a few different libraries available to solve your problem (all of them follow the second approach with a single RecyclerView), but most come with a lot of extra features which you may not need:

    RendererRecyclerViewAdapter

    It comes with a ViewRenderer/ViewModel interface, which works like a "partial" RecyclerView for a single item type. You would create one for every item type and then register them in a single adapter.

    Epoxy

    A library/framework create by airbnb and used heavily in their app. They have a lot of scrollable content (similar to a web page) with a lot of different item types. Epoxy also helps with the composition of the different items on a page, and it handles animations when the content or its order changes. Too much if you only need it for a single screen.

    Litho

    A complete UI framework created by Facebook which comes with it's own rendering engine, a replacement for xml layouts and much more. As far as I understand, it allows you to do to handle large amounts of items (like the Facebook timeline) and the view recycling is handled automatically. Like Epoxy, you would only use this if your app includes things like endless scrolling with a lot of different item types and you really need the performance.


    I tried Epoxy and RendererRecyclerViewAdapter, but after all I created my own multiple item type adapter. It can be created in less than 100 lines of code.

    0 讨论(0)
提交回复
热议问题