【问题标题】:Place a recyclerview between 2 views在 2 个视图之间放置一个回收站视图
【发布时间】:2023-03-25 04:23:01
【问题描述】:

我正在尝试实现这种布局

<RelativeLayout>
    <LinearLayout> <!-- Always attached to the top of the parent -->
    <RecyclerView>
    <LinearLayout> <!-- Always attached to the bottom of the parent -->   
    <GridView>
<RelativeLayout>

这是在相对布局中的两个线性布局之间具有回收器视图的完整代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">    
    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="160dp"/>    
        <LinearLayout
            android:id="@+id/layoutDots"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_alignBottom="@id/view_pager"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:orientation="horizontal" />
        <View android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_below="@id/view_pager"
            android:background="@android:color/darker_gray" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_below="@id/LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">    
       <View android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:layout_margin="10dp"
            android:background="@android:color/darker_gray" />
        <TextView
            android:id="@+id/orLoginWithEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical"
            android:text="Shop By Category"
            android:textColor="@color/black"
            android:textSize="16sp" />
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:background="@android:color/darker_gray" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/LinearLayout1"
        android:layout_below="@id/LinearLayout"
        android:background="@android:color/white"
        android:orientation="horizontal"/>
    <GridView
        android:id="@+id/gridview"
        android:layout_below="@id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:horizontalSpacing="10dp"
        android:numColumns="4"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingTop="10dp"
        android:verticalSpacing="20dp"></GridView>    
    </RelativeLayout>

我试图在父相对布局内的两个线性布局之间放置回收器视图,但它没有显示回收器视图。我认为它与某些视图重叠或可能是任何其他问题。我当前的布局输出是

【问题讨论】:

  • 你想要 Linear -> recylerView -> LinearLayout 从上到下的顺序对吗?
  • 您是否需要 Recyclerview 和 GridView 自己在里面滚动,或者您想滚动 recyclerview 的所有项目然后显示“按类别购物”然后启动 GridView?

标签: android android-layout listview


【解决方案1】:

添加此代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<LinearLayout
    android:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="160dp" />

    <LinearLayout
        android:id="@+id/layoutDots"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:orientation="horizontal" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@android:color/darker_gray" />
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/listView">

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_gravity="center_vertical"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/orLoginWithEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center_vertical"
        android:text="Shop By Category"
        android:textColor="#000000"
        android:textSize="16sp" />

    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_gravity="center_vertical"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:background="@android:color/darker_gray" />
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/LinearLayout"
    android:background="@android:color/white"
    android:orientation="horizontal" />

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/LinearLayout1"
    android:horizontalSpacing="10dp"
    android:numColumns="4"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    android:paddingTop="10dp"
    android:verticalSpacing="20dp" />

【讨论】:

    【解决方案2】:

    您应该使用更高效的ConstraintLayout

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/linear_1"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:background="@android:color/black"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    
        <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@android:color/holo_blue_bright"
            app:layout_constraintBottom_toTopOf="@+id/linear_2"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linear_1"/>
    
        <LinearLayout
            android:id="@+id/linear_2"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:background="@android:color/black"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/grid_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    
        <GridView
            android:id="@+id/grid_view"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:background="@android:color/holo_green_dark"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>
    </android.support.constraint.ConstraintLayout>
    

    并将compile 'com.android.support.constraint:constraint-layout:1.0.2' 添加到您的 gradle 文件中。

    看起来像这样

    黑色 -> 线性布局

    蓝色 -> RecyclerView

    绿色 -> GridView

    【讨论】:

      【解决方案3】:

      使用对齐属性

      要将 childLayout 放置在 RelativeLayout 的顶部,请使用 alignParentTop = "true"
      要将 childLayout 放置在 RelativeLayout 的底部,请使用 alignParentBottom = "true"
      在您的情况下,分别在您的子 LinearLayouts 中给出上述属性。并确保 RecyclerView 位于第一个 LinearLayout 之下 在 RecyclerView 中使用此属性android:layout_below = "@id/LinearLayout"

      【讨论】:

        【解决方案4】:

        仅使用 RecyclerView 并使用以下代码:

        recyclerView .setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 8);
        recyclerView .setLayoutManager(layoutManager);
        

        // 8 是要显示的列数

        【讨论】: