Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

android - Two RecyclerViews under each other in one layout

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...