彼此下方的 Recycler View 未显示?

Recycler View below each other not showing up?

我有两个简单的回收器视图,我想直接在彼此下方显示。这是我的布局:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/drawer_view_header"
            android:id="@+id/navigation_header"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/navigation_header"
            android:id="@+id/friends_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/friends_list"
            android:id="@+id/followers_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

我的 navigationHeader 在我的第一个回收器视图 friends_list 上面,它工作正常,我可以看到 friends_list 回收器视图有 android:layout_height="wrap_content" 但是 followers_list 回收站视图似乎没有显示,即使我的 friends_list 的所有内容都已显示。任何想法为什么它没有出现?谢谢!

您必须为 recyclerview 设置高度 android 提供权重以在不同屏幕尺寸下进行调整

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2">

    <include
        layout="@layout/drawer_view_header"
        android:id="@+id/navigation_header"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/navigation_header"
        android:id="@+id/friends_list"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/friends_list"
        android:id="@+id/followers_list"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"/>

</RelativeLayout>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include
                layout="@layout/drawer_view_header"
                android:id="@+id/navigation_header"/>

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

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

   </LinearLayout>

因为第一个 RecyclerView 的高度是 wrap_content,你的第二个 RecyclerView 不会出现,第一个 RecyclerView 将占据你布局的所有高度,如果你里面有更多的项目。

要解决您的问题,请使用 LinearLayout 作为 root 并为您的两个 RecyclerView 提供相等的 layout_weightlayout_height="0dp"

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/drawer_view_header"
        android:id="@+id/navigation_header"/>

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/navigation_header"
        android:id="@+id/friends_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"    
        android:layout_weight="1"/>    <---

    <android.support.v7.widget.RecyclerView
        android:layout_below="@id/friends_list"
        android:id="@+id/followers_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"   
        android:layout_weight="1"/>    <---

</LinearLayout>

这将显示两个占用相同 space(高度)的 RecyclerView。

虽然上述答案确实有效,但它们不会保留回收站视图的包装内容行为,因为您需要使用 NestedScrollView.

例如你需要做这样的事情:-

 <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            layout="@layout/drawer_view_header"
            android:id="@+id/navigation_header"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/navigation_header"
            android:id="@+id/friends_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/friends_list"
            android:id="@+id/followers_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

或者您也可以在 NestedScrollView 中使用垂直对齐的 LinearLayout

*注意:- 这仅适用于 23.2.0 以上的回收站视图

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

尝试 this.It 有效。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:paddingBottom="10dp"
                android:text="Asia"
                android:textStyle="bold" />

        <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

                <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"


             android:layout_marginTop="@dimen/activity_horizontal_margin"
                android:gravity="center_vertical"
                android:paddingBottom="10dp"
                android:text="Europe"
                android:textStyle="bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/cardview"
                android:textColor="#fff"
                android:layout_weight="1"
                android:id="@+id/select"
                android:text="Select all"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="5dp"
                android:background="@drawable/cardview"
                android:textColor="#fff"
                android:layout_weight="1"
                android:id="@+id/deselect"
                android:text="Deselct all"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:background="@drawable/cardview"
                android:textColor="#fff"
                android:id="@+id/next"
                android:visibility="visible"
                android:text="Next activity"/>

        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.NestedScrollView>