14

I'm trying to show horizontal recyclerview items and vertical recyclerview items inside an ScrollView

Scrollview didn't work even If i use android:fillViewport="true"

  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    //Horizontal Recyclerview items
    <RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RecyclerView>
    //vertical Recyclerview items
        <RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"></RecyclerView>
    </LinearLayout>

</ScrollView>
AlgoCoder
  • 1,706
  • 5
  • 19
  • 40
  • 1
    You layout could be simplified. ParentView is a `RecyclerView` (vertical items) and the item at index 0 is another `RecyclerView` (horizontal items). – Charles Durham Dec 16 '15 at 17:34
  • you should have to use one child of scrollview and other views of your xml will be in that single child. for recyclerView's to work with scrollview, you have to give 0dp height to your recyclerview in xml and provide the max height that your recyclerview's can take at the runtime. You can calculate max height by multiplying the total number of rows * height of one row (in dp) in case of vertical recyclerView and in horizontal you can give height of one child only. Like if we have 10 textview of 40dp each, then vertical recyclerView height will be 10*40 dp = 400dp and 10dp for horizontal. – Vatish Sharma Feb 04 '16 at 10:35
  • you can do it... look here : [link](http://stackoverflow.com/a/36979664/4240256) – Ester Kaufman May 02 '16 at 09:58
  • 1
    For future reference, if anybody is experiencing `RecyclerView` wrap_content issue inside `ScrollView` that **only** happens on marshmallow/nougat (API 23, 24) devices, check my workaround at http://stackoverflow.com/a/38995399/132121 – Hossain Khan Aug 17 '16 at 11:35

3 Answers3

13

You should use NestedScrollView instead. However you may need to write your own LayoutManager. Check out this SO Answer for more details

Community
  • 1
  • 1
ashkhn
  • 1,642
  • 1
  • 13
  • 18
4
  1. You need to use a custom layout manager to use recyclerview inside a scrollview.

  2. You can remove the scrollview and make a header item in the vertical recyclerview which contain the horizontal recyclerview.

Also you should not use a recyclerview inside a scrollview. So think the second approach will be better.

Also you can can use Snap-RecyclerView-Utils. It has a linear layout manager for recyclerview inside a scroll view and an adapter which can help you make a header containing you horizontal recyclerview.

Prashant Solanki
  • 660
  • 5
  • 10
2

ScrollView can only have one child.
Remove your RelativeLayout and try again.

Apart from that android:layout_height in ScrollView should be set to wrap_content

Additionally I'm not quite sure, if it works, since in the Docs it is stated that

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

Maybe a NestedScrollView works since it is for

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.

Desdroid
  • 300
  • 2
  • 9