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?
It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.
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.
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.
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.
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:
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With