bircastri
bircastri

Reputation: 2165

How can I display two RecycleViews in one Activity?

I want to display in my activity Android, a modal that contains two RecycleView. So I have build this xml file as modal:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    android:background="@drawable/sfondo_app"
    tools:context=".it.eresult.decipher.activity.AlertsActivity">

    <TextView
        android:id="@+id/labelAgent"
        android:textColor="@color/white"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List of Agent "
        android:background="@color/colorHeaderTable"
        />



        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_agent_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:layout_marginTop="28dp"/>




    <TextView
        android:id="@+id/labelReaction"
        android:textColor="@color/white"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List of Reaction "
        android:background="@color/colorHeaderTable"
        android:layout_marginTop="30dp"
        />


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_reaction_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:layout_marginTop="28dp"/>

</RelativeLayout>

With this code I can see the two RecycleView are superimposed, instead I want to display Text, under RecycleView, under Text, under the second Recycle view.

It is possible to do this?

Upvotes: 0

Views: 121

Answers (3)

AmirG
AmirG

Reputation: 625

I think the best aproach is using a LinearLayout with vertical orientation instead of RelativeLayout, but if you are using RelativeLayout as root layout you should do somethinge like:

<RelativeLayout ... >

<TextView
    android:id="@+id/labelAgent"
    ...
    />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_agent_view"
        android:layout_below="@+id/labelAgent"
        />

<TextView
    android:id="@+id/labelReaction"
    android:layout_below="@+id/recycler_agent_view"
    />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_reaction_view"
    android:layout_below="@+id/labelReaction"
   /> 
</RelativeLayout>

Upvotes: 0

Atiq
Atiq

Reputation: 14825

use a LinearLayout as parent to your both RecyclerViews

You can also assign andorid:weight_sum ="1" to both RecyclerViews

it will make sure your both RecyclerViews takes the same size regardless of how big the screen size of phone is.

Sample:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    android:background="@drawable/sfondo_app"
    tools:context=".it.eresult.decipher.activity.AlertsActivity">

    <TextView
        android:id="@+id/labelAgent"
        android:textColor="@color/white"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List of Agent "
        android:background="@color/colorHeaderTable"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_agent_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_marginTop="28dp"/>

    <TextView
        android:id="@+id/labelReaction"
        android:textColor="@color/white"
        android:textSize="18dp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List of Reaction "
        android:background="@color/colorHeaderTable"
        android:layout_marginTop="30dp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_reaction_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_marginTop="28dp"/>
</LinearLayout>

Upvotes: 1

Umesh Singh Kushwaha
Umesh Singh Kushwaha

Reputation: 5741

You should use relative percentage layout that is provided by support library. Easy to use. For example

 <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout>

http://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

Upvotes: 0

Related Questions