同时可见一个片段中的多个回收器视图

时间:2016-04-02 11:54:13

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

enter image description here 我正在尝试将两个回收站视图添加到单个布局中,该布局位于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>

3 个答案:

答案 0 :(得分:0)

避免在一个xml文件中使用太多布局容器(如Linera Layout),过度绘制最终会导致内存问题。 您可以使用一个LinearLayout并将RecyclerViews的高度设置为0dp,将layout_weight设置为&#34; 1&#34;:

<?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>

答案 1 :(得分:0)

设置LinearLayout权重          

        <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>

答案 2 :(得分:0)

使用 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