我在一个Fragment中有两个RecyclerView,但仅显示第一个。

3
我正在尝试在一个Fragment中使用两个RecyclerView。其中一个是水平对齐的RecyclerView,另一个是垂直对齐的RecyclerView。但只有水平对齐的RecyclerView可见。(如果我在布局文件中更改RecyclerViews的顺序,则可以看到垂直对齐的RecyclerView,因此我相信RecyclerViews没有问题。)
根据其他类似问题的答案,我已经在两个RecyclerView中都添加了"layout_weight=1"。但第二个RecyclerView仍然不显示。
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/CategoryPageRecyclerView"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:scrollbars="none" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/ItemsRecyclerView"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:scrollbars="vertical" />

        </android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>

我觉得SwipeRefreshLayout可能会有问题。你有什么想法可以修复它吗?

1个回答

4

SwipeRefreshView只支持一个直接子元素 - 将RecyclerView包装在LinearLayout中,它就可以工作了。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/CategoryPageRecyclerView"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:scrollbars="none" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/ItemsRecyclerView"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="0dp"
                android:scrollbars="vertical" />

        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

希望这能对你有所帮助,如果有任何问题,请留言。

它确实有效!非常感谢 :) 顺便问一下,我如何避免将一半窗口分配给水平RecyclerView? - Yashit
编辑您的权重或完全删除它们,并将第一个的高度设置为dp中的某个值(android:layout_height="100dp"),将第二个设置为match_parent - Markaos
您可以接受答案,这样其他人在找到此问题时就会立即知道该答案适用于您的情况 - 它位于投票按钮下方。 - Markaos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接