1

I have two simple recycler views that I want to show directly below each other. Here is my layout:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/drawer_view_header"
            android:id="@+id/navigation_header"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/navigation_header"
            android:id="@+id/friends_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/friends_list"
            android:id="@+id/followers_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

I have my navigationHeader above my first recycler view called friends_list which works fine, and I can see the friends_list recycler view which has android:layout_height="wrap_content" but the followers_list recycler view doesn't seem to show even though all the contents of my friends_list is all shown. Any ideas why it is not showing up? Thanks!

user1871869
  • 3,317
  • 13
  • 56
  • 106
  • Using wrap_content won't work. You'll have to change the RelativeLayout to a vertical LinearLayout and set the RecyclerViews height using a layout_weight of 1 and layout_height of 0dp for both. – the-ginger-geek Jun 07 '16 at 04:48
  • it can be happen if you set height to match parent in on`onCreateViewHolder` infalted layout of friend list recycleview – Burhanuddin Rashid Jun 07 '16 at 04:50
  • @Neil I understand what you are trying to say but I want it so that my `friends_list` recycler view appears below the `followers_list` recycler view. If I set the weight to 1 then each take up half the screen, but if `friends_list` is not filled up all the way there is a big blank space between that and the `followers_list` – user1871869 Jun 07 '16 at 04:53
  • @Neil oh I see what you are saying, then is there no way to show these recycler views with one underneath the other easily then aside from what you just suggested? – user1871869 Jun 07 '16 at 04:58
  • @user1871869 Check with this [answer](http://stackoverflow.com/a/30081772/2078074). You may help from this. – Dhruv Jun 07 '16 at 05:00

5 Answers5

4

While the above answers do work they don't preserve the wrap content behavior of recycler view for that you need to use a NestedScrollView.

For eg you need to do something like this:-

 <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/drawer_view_header"
            android:id="@+id/navigation_header"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/navigation_header"
            android:id="@+id/friends_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/friends_list"
            android:id="@+id/followers_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

Or you may also use a LinearLayout with vertical alignment in your NestedScrollView.

*Note:- this will only work with recycler view above 23.2.0

compile 'com.android.support:recyclerview-v7:23.2.0'
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
0

You must set height for the recyclerview android provide weight to view to adjust in different screen size

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2">

    <include
        layout="@layout/drawer_view_header"
        android:id="@+id/navigation_header"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/navigation_header"
        android:id="@+id/friends_list"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/friends_list"
        android:id="@+id/followers_list"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"/>

</RelativeLayout>
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
  • What if I don't want to hard code the layout_height? I would prefer not to because depending on the size of the recycler view I would block certain contents – user1871869 Jun 07 '16 at 04:50
  • if you dont want to hard code layout_height, you can use set 'android:layout_height="0dp" android:layout_weight="1"` instead. this will adjust the height. – SaravInfern Jun 07 '16 at 04:56
0
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include
                layout="@layout/drawer_view_header"
                android:id="@+id/navigation_header"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/friends_list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/followers_list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

   </LinearLayout>
Manish Jain
  • 2,139
  • 19
  • 15
0

Since height of first RecyclerView is wrap_content, your second RecyclerView will not be showing up, first RecyclerView will occupy all the height of your layout if you have more items in it.

To solve your problem, use LinearLayout as root and provide equal layout_weight and layout_height="0dp" to both of your RecyclerViews.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/drawer_view_header"
        android:id="@+id/navigation_header"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/navigation_header"
        android:id="@+id/friends_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"    
        android:layout_weight="1"/>    <---

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/friends_list"
        android:id="@+id/followers_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"   
        android:layout_weight="1"/>    <---

</LinearLayout>

This will show two RercyclerViews with same space (height) occupied.

Apurva
  • 7,871
  • 7
  • 40
  • 59
  • have a look at this one too: http://stackoverflow.com/questions/37977521/how-to-place-adview-below-the-recyclerview-please-see-details PLEASE! – Hammad Nasir Jun 23 '16 at 18:56
0

Try this.It is working.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingBottom="10dp"
                android:text="Asia"
                android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

                <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"


             android:layout_marginTop="@dimen/activity_horizontal_margin"
                android:gravity="center_vertical"
                android:paddingBottom="10dp"
                android:text="Europe"
                android:textStyle="bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/cardview"
                android:textColor="#fff"
                android:layout_weight="1"
                android:id="@+id/select"
                android:text="Select all"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="5dp"
                android:background="@drawable/cardview"
                android:textColor="#fff"
                android:layout_weight="1"
                android:id="@+id/deselect"
                android:text="Deselct all"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:background="@drawable/cardview"
                android:textColor="#fff"
                android:id="@+id/next"
                android:visibility="visible"
                android:text="Next activity"/>

        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.NestedScrollView>
thebadassdev
  • 156
  • 9