Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having RecyclerView inside a NestedScrollView calls onBindView for all the items

Tags:

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 LinearLayout inside NestedScrollView

This is the my layout file.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" android:scrollbars="none">  <LinearLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical">      <android.support.v7.widget.RecyclerView         android:id="@+id/featured_list"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <android.support.v7.widget.RecyclerView         android:id="@+id/all_topic_list"         android:layout_width="match_parent"         android:layout_height="wrap_content" /> </LinearLayout> 

Also, I am disabling nested scrolling in Java code.

disableNestedScrolling(findViewById(R.id.all_topic_list)); disableNestedScrolling(findViewById(R.id.featured_list)); 

My RecylerView library version is 26.1.0

This works fine perfectly, but then onBindViewHolder method is getting called for all the items in the list. Ideally it should only be called for the visible items in the list.

I think the issue is happening because I am giving wrap_content to the RecyclerView. A lot of answers on this question suggest that the issue is solved in v23.2.1, but I am already using v26.1.0. How to solve this issue?

like image 590
thedarkpassenger Avatar asked Jun 02 '18 09:06

thedarkpassenger


People also ask

How do you load all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.

What is difference between nested ScrollView and NestedScrollView?

Nested scrolling is enabled by default. Show activity on this post. NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView. But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed.

Is a recycler view scrollable?

To help you build apps with lists, Android provides the RecyclerView . RecyclerView is designed to be very efficient, even with large lists, by reusing, or recycling, the views that have scrolled off the screen.

What is the difference between ScrollView and RecyclerView?

In the practical on scrolling views, you use ScrollView to scroll a View or ViewGroup . ScrollView is easy to use, but it's not recommended for long, scrollable lists. RecyclerView is a subclass of ViewGroup and is a more resource-efficient way to display scrollable lists.


1 Answers

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.

like image 50
boformer Avatar answered Sep 19 '22 13:09

boformer