在一个布局中两个RecyclerView在彼此之下 [英] Two RecyclerViews under each other in one layout

查看:117
本文介绍了在一个布局中两个RecyclerView在彼此之下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一个布局中互相获得两个RecyclerView?我不希望所有项目都有一个RecyclerView. 我的代码:

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>

推荐答案

我自己找到了答案.

您需要将LinearLayout放入 ScrollView ,并将wrap_content用作RecyclerView的layout_height.

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>

RecyclerView和wrap_content也存在一个错误,因此您必须使用自定义布局管理器.看看这篇文章:如何使WRAP_CONTENT起作用一个RecyclerView

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

这篇关于在一个布局中两个RecyclerView在彼此之下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆