Having RecyclerView inside a NestedScrollView calls onBindView for all the items

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


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.