40

How can I get two RecyclerViews under each other in one layout? I don't want to have a single RecyclerView for all items. My code:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/main__item_background"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TextView
    android:text="@string/find_friends__already_playing"
    android:background="@color/header"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_header"
    android:visibility="visible"/>

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

<TextView
    android:text="@string/find_friends__invite_friends"
    android:background="@color/find_friends__header"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_header" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/friends_to_invite"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />
</LinearLayout>
alan_derua
  • 2,252
  • 3
  • 20
  • 19

7 Answers7

42

I've found the answer myself.

You need to put the LinearLayout into a ScrollView and use wrap_content as RecyclerView's layout_height.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_header"
        android:background="@color/header"
        android:gravity="center"
        android:text="@string/find_friends__already_playing"
        android:visibility="visible" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/in_app_friends"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@color/white"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_header"
        android:background="@color/find_friends__header"
        android:gravity="center"
        android:text="@string/find_friends__invite_friends" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/friends_to_invite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"/>
</LinearLayout>
</ScrollView>

Also there is a bug with with RecyclerView and wrap_content so you have to use a custom layout manager. Check out this post: How do I make WRAP_CONTENT work on a RecyclerView

Community
  • 1
  • 1
alan_derua
  • 2,252
  • 3
  • 20
  • 19
  • 10
    You should just be using one `RecyclerView` with different item view types. `RecyclerView.Adapter` has a `getItemViewType(int position)` that will be passed into `onCreateViewHolder(ViewGroup parent, int itemtype)` where you can switch on the type and create the correct `RecyclerView.ViewHolder`. – Charles Durham May 06 '15 at 16:21
  • 7
    What should I do, when I have to use different models in the recyclerviews? – alan_derua May 07 '15 at 13:52
  • 2
    `wrap_content` is now **fixed** in the latest Android Support Library **no need** to write a custom layout manager – André Delgado Apr 13 '16 at 18:11
  • 5
    for smooth scrolling it's better to set also recyclerView.setNestedScrollingEnabled(false) on each one of the recycler views – Presen May 08 '17 at 08:59
  • 1
    The last point is outdated: [starting from **AppCompat v23.2**, all built-in LayoutManagers now support auto-measurement](https://android-developers.googleblog.com/2016/02/android-support-library-232.html#recyclerview). `Previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible.` – PLNech Apr 04 '18 at 12:20
  • 2
    best solution is use nested scrollview – SAndroidD May 11 '18 at 06:30
  • 1
    android's documentation explicitly [advises against](https://developer.android.com/reference/android/widget/ScrollView) adding `RecyclerView` or `ListView` inside a `ScrollView`. – andersonvom Mar 12 '19 at 15:17
35

You should create an XML layout file like this

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

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

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/steps_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

And in the code, you should call setNestedScrollingEnabled(false)

RecyclerView ingredientsList = findViewById(R.id.ingredients_list);
RecyclerView stepsList = findViewById(R.id.steps_list);

ingredientsList.setNestedScrollingEnabled(false);
stepsList.setNestedScrollingEnabled(false);
Ahmet Türk
  • 358
  • 3
  • 5
9

I also had the same problem and wrote a library which helps to achieve this by joining adapters and layouts.

Gradle dependency to try it (needs jcenter repo):

compile 'su.j2e:rv-joiner:1.0.3'//latest version by now

Thea change xml to use a single RecyclerView which matches parent:

<android.support.v7.widget.RecyclerView
    android:id="@+id/joined_friends_rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:background="@color/white"/>

Then init RecyclerView in code like this:

    //init your RecyclerView as usual
    RecyclerView rv = (RecyclerView) findViewById(R.id.joined_friends_rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    //construct a joiner
    RvJoiner rvJoiner = new RvJoiner();
    rvJoiner.add(new JoinableLayout(R.layout.your_title_for_in_app));
    rvJoiner.add(new JoinableAdapter(new YourInAppRvAdapter()));
    rvJoiner.add(new JoinableLayout(R.layout.your_title_for_invite));
    rvJoiner.add(new JoinableAdapter(new YourInviteRvAdapter()));

    //set join adapter to your RecyclerView
    rv.setAdapter(rvJoiner.getAdapter());

You can check this link for more library details and explanation. Hope it helps.

j2esu
  • 1,647
  • 1
  • 16
  • 26
  • Adding this library via build.gradle creates error while syncing – Reaz Murshed Mar 27 '16 at 13:12
  • hey @ReazMurshed can you please help me with this https://stackoverflow.com/questions/49952965/recyclerview-horizontal-scrolling-to-left?noredirect=1#comment87836903_49952965 –  May 18 '18 at 12:34
5

if you get the bottom recyclerview not scrolling with the main content, change the LinearLayout (see answer from alan_derua) to ConstraintLayout and wrap the two RecyclerViews inside. See code below:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">

<android.support.constraint.ConstraintLayout
    android:id="@+id/task_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/first_list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:gravity="left"
        android:paddingTop="0dp"
        android:text="@string/my_tasks"
        app:layout_constraintBottom_toTopOf="@+id/second_list_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/first_list_view" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/second_list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

</android.support.constraint.ConstraintLayout>
</ScrollView>

This worked for me!

Miki
  • 329
  • 4
  • 10
3

You can give each RecycleView height equal to 0dp and weight equal 1:

android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
questioner
  • 2,283
  • 3
  • 26
  • 35
  • 3
    I don't want to them both tacking the half of the screen. I want them to be scrolled one after each other. – alan_derua May 06 '15 at 12:46
1

Use NestedScrollView as parent layout, it should have

android:weightSum="2"

and give

android:layout_weight="1"

to each RecyclerView of yours.It should be scrolled one after each other.

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
0

Just use:

<android.support.v7.widget.RecyclerView
    android:id="@+id/card_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:scrollbars="vertical"
    android:layout_below="@+id/your_first_recycler"/>

last line is for your problem.use it.

mehrdad
  • 33
  • 1
  • 3