【问题标题】:Multiple recycler view in one fragment visible simultaneously一个片段中的多个回收器视图同时可见
【发布时间】:2016-07-22 05:42:17
【问题描述】:

我正在尝试将两个回收器视图添加到单个布局中,该布局位于 Fragment 中。但我只能看到第一个RecyclerView。 是否可以像上图一样添加两个回收器视图(两者都需要同时可见),或者android中是否存在某种限制,只能将一个回收器视图添加到一个完整的屏幕??

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/test_recycle1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#FFCC00"
        android:dividerHeight="4px"
        android:background="#e0e0e0"/>
    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/test_recycle2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#FFCC00"
        android:dividerHeight="4px"
        android:background="#e0e0e0"/>
        </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 显示您的 xml。这可以通过 LinearLayout 和权重来完成。

标签: android xml android-studio android-fragments android-recyclerview


【解决方案1】:

使用 android:weightSum 解决您的问题:

检查下面的布局

<?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:weightSum="2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_recycle1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#e0e0e0"
            android:divider="#FFCC00"
            android:dividerHeight="4px" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_recycle2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#e0e0e0"
            android:divider="#FFCC00"
            android:dividerHeight="4px" />
    </LinearLayout>

</LinearLayout>

看看http://developer.android.com/guide/topics/ui/layout/linear.html

【讨论】:

  • 不,问题没有解决,当我在一个带有权重的布局上使用两个回收器视图时,第二个回收器视图根本没有加载。只有一个空格
【解决方案2】:

设置线性布局权重

        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_recycle1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#FFCC00"
            android:dividerHeight="4px"
            android:layout_weight="1"
            android:background="#e0e0e0"/><!---->



        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_recycle2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#FFCC00"
            android:layout_marginTop="20dp"
            android:dividerHeight="4px"
            android:layout_weight="1"
            android:background="#e0e0e0"/>


</LinearLayout>

【讨论】:

    【解决方案3】:

    避免在一个xml文件中使用太多像Linera Layout这样的Layout Container,过度绘制最终会导致内存问题。 您的问题可以通过使用一个LinearLayout 并将RecyclerViews 的高度设置为0dp 并将layout_weight 设置为“1”来解决:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_recycle1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="#FFCC00"
            android:dividerHeight="4px"
            android:background="#e0e0e0"/>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/test_recycle2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:divider="#FFCC00"
            android:dividerHeight="4px"
            android:background="#e0e0e0"/>
    </LinearLayout>
    

    【讨论】:

    • 我会试试这个并回复你
    • 它不工作:(当我在一个带有权重的布局上使用两个回收器视图时,第二个回收器视图根本没有加载。只有一个空白空间
    • 您的第二个 RecyclerView 中有数据吗?您是否在两个 RecyclerView 旁边的布局中添加了任何内容?
    • 哈哈 :D 当然我添加了数据!!!为两者创建了 2 个不同的模型和适配器,创建了一个列表,将其传递给适配器,为两个回收器视图设置适配器,但只加载第一个列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2020-12-25
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多