Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: RecyclerView inside a ScrollView

I have a RecyclerView wrapped in a LinearLayout and it works perfectly as expected. I can see all the data in the RecyclerView as populated. So far so good.

When I wrap the LinearLayout in a ScrollView, the RecyclerView goes blank. I do not see anything inside RecyclerView. Why? How to make this work.

The page is one of the tabs in a ViewPagerIndicator, so everything in that tab needs to be in a ScrollView.

Thanks for all the help.

like image 361
nomongo Avatar asked Aug 26 '14 22:08

nomongo


People also ask

What is nested scroll view in Android?

NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

How can show all items in RecyclerView without scrolling?

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

How do I stop NestedScrollView scrolling?

setnestedscrollingenabled set it to false.

How do I make my recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .


2 Answers

Set this property for the ScrollView,

 android:fillViewport="true"

ScrollView will extend itself to fill the contents

like image 62
Sathesh Avatar answered Oct 07 '22 10:10

Sathesh


After checking implementation, the reason appears to be the following. If RecyclerView gets put into a ScrollView, then during measure step its height is unspecified (because ScrollView allows any height) and, as a result, gets equal to minimum height (as per implementation) which is apparently zero.

You have couple of options for fixing this:

  • Set a certain height to RecyclerView
  • Set ScrollView.fillViewport to true
  • Or keep RecyclerView outside of ScrollView. I my opinion, this is the best option by far. If RecyclerView height is not limited - which is the case when it's put into ScrollView - then all Adapter's views have enough place vertically and get created all at once. There is no view recycling anymore which kinda breaks the purpose of RecyclerView.
like image 32
sergej shafarenka Avatar answered Oct 07 '22 11:10

sergej shafarenka