【问题标题】:I have 2 RecyclerViews in a Fragment. But only the 1st one is shown我在一个片段中有 2 个 RecyclerViews。但只显示第一个
【发布时间】:2017-06-09 20:24:05
【问题描述】:

我试图在一个片段中有两个 RecyclerViews。其中一个是水平对齐的 RecyclerView,另一个是垂直对齐的。但是只有水平的 RecyclerView 是可见的。 (如果我在布局文件中更改 RecyclerViews 的顺序,垂直 RecyclerView 是可见的,所以我很确定 RecyclerViews 没有任何问题。)

根据类似问题的其他答案,我在两个 RecyclerViews 中都添加了 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 引起了问题。有关如何解决此问题的任何想法?

【问题讨论】:

    标签: java android android-fragments android-recyclerview swiperefreshlayout


    【解决方案1】:

    SwipeRefreshView 仅支持一个直接子级 - 将 RecyclerViews 包裹在 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?
    • 编辑您的权重或完全删除它们,并将第一个高度设置为 dp (android:layout_height="100dp") 和第二个到 match_parent
    • 您可以接受答案,这样发现此问题的其他人会立即知道答案在您的情况下有效 - 它位于投票按钮下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多