Wai Yan Hein Wai Yan Hein - 4 months ago 241
Android Question

Two RecyclerViews in one layout is not working cause one is missing in Android

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


So please how can I fix my code to use two RecyclerViews in single layout. Please is that possible? Please help me. Is there any better way to do it please?

Answer

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>