0

I am developing an Android app. In my app, I want to use two RecyclerViews in one layout. I used LinearLayout to wrap up RecyclerViews because of this Stack Overflow question (Two RecyclerViews under each other in one layout).

As you can see the answer says, to use LinearLayout and set RecyclerViews height to wrap_content. I followed it. But when I run only one RecyclerView is appear and one is missing.

This is the screenshot:

enter image description here

As you can see, only on RecyclerView is appeared.

This is my XML layout:

<LinearLayout
        android:orientation="vertical"
         app:layout_behavior="@string/appbar_scrolling_view_behavior"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
         <android.support.v7.widget.RecyclerView
             android:scrollbars="vertical"
             android:id="@+id/ai_rc_reviews"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
        <android.support.v7.widget.RecyclerView
            android:scrollbars="vertical"
            android:id="@+id/ai_rc_reviews_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
     </LinearLayout>

I tried this as well. Nothing appears on screen:

<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" >

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


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

How can I fix my code to use two RecyclerViews in single layout. Is there any better way to do it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

5 Answers5

2

Try using the layout_weight property instead of wrap_content. Give both RecyclerViews same weight and change height to 0dp.

<LinearLayout
     android:orientation="vertical"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

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

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

</LinearLayout>
Carlos J
  • 2,965
  • 4
  • 17
  • 28
  • It works. But having a problem with that. I have to set the height, margin and padding and so on using weight attribute. So it is not precise. What I want is I want second RecyclerView directly after first one. No matter how many items it has or how height it is. Is that posible? – Wai Yan Hein May 31 '16 at 20:47
  • I'm not sure if it will work, but you can try leaving the top `RecyclerView` weight as 1 and remove the weight property on the bottom `RecyclerView`. Also change `android:layout_height ` to wrap_content on the bottom one too. Give it a try. – Carlos J May 31 '16 at 21:08
  • No that is not working. Whenver I want to upvote the answer cause I was able to add two recyclerviews to a single layout. But I changed the design cause I feel using two recyclerviews is not good and do not have full control over design layout. Thanks for ur great answer. – Wai Yan Hein Jun 01 '16 at 07:40
0

Generally it's not a good idea to have two scroll containers one inside the other if they scroll along the same axis. That behavior will most likely cause confusion with the user. Instead, if you are 100% sure that you need to have two scrolling containers vertically stacked - and please try not to do it if not 100% necessary, you should use fixed heights for each container as it will positively impact overall performance of the created layout.

milosmns
  • 3,595
  • 4
  • 36
  • 48
0

    <!-- Usual Size -->
    <LinearLayout
        android:id="@+id/usualSize_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical">
        <TextView
            android:id="@+id/usualSize_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="size"
            android:textSize="@dimen/font_15"
            android:textColor="@color/color_999999"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/usualSize_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height"/>
    </LinearLayout>

    <!-- MarkImage  -->
    <LinearLayout
        android:id="@+id/mark_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical">
        <TextView
            android:id="@+id/mark_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mark"
            android:textColor="@color/color_999999"
            android:textSize="@dimen/font_15"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"/>

    <android.support.v7.widget.RecyclerView
            android:id="@+id/mark_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height"/>
    </LinearLayout>
</LinearLayout>

Here we should think twice the WRAP_CONTENY, so we should in the file:build.gradle.

Please update version of a library in gradle file :

compile com.android.support:recyclerview-v7:23.2.1

and higher version. please check your version.

How do I make WRAP_CONTENT work on a RecyclerView

Community
  • 1
  • 1
BertKing
  • 513
  • 5
  • 13
0
<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="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
  android:scrollbars="none"
 app:layout_behavior="@string/appbar_scrolling_view_behavior">
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <!-- Usual Size -->
    <LinearLayout
        android:id="@+id/usualSize_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/usualSize_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="size"
            android:textSize="@dimen/font_15"
            android:textColor="@color/color_999999"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"
            />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/usualSize_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height"

            />
    </LinearLayout>

    <!-- MarkImage  -->
    <LinearLayout
        android:id="@+id/mark_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_f5f5f5"
        android:layout_marginLeft="@dimen/common_10"
        android:layout_marginTop="@dimen/common_10"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/mark_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="mark"
            android:textColor="@color/color_999999"
            android:textSize="@dimen/font_15"
            android:layout_marginTop="@dimen/common_16"
            android:layout_marginBottom="@dimen/common_10"
            />`
       <android.support.v7.widget.RecyclerView
            android:id="@+id/mark_group"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingBottom="@dimen/list_height" />
        </LinearLayout>
      </LinearLayout>
    </ScrollView>

Here we should think twice the WRAP_CONTENY ,so we should in the file:build.gradle.

Please update version of a library in gradle file :

compile 'com.android.support:recyclerview-v7:23.2.1'

and higher version.please check your version.

How do I make WRAP_CONTENT work on a RecyclerView

Community
  • 1
  • 1
BertKing
  • 513
  • 5
  • 13
0

Just in case someone has two RecyclerViews and other views inside one Srcollview, if you have problem that the first recyclerview won't scroll up or second one part missing,

if you used LinearLayout to contain these RecyclerViews , try to use relativeLayout instead, which solved my problem.

Xianwei
  • 2,381
  • 2
  • 22
  • 26