如何在GridLayoutManager中使项目的高度相对于另一个项目进行调整?

时间:2019-09-12 23:25:15

标签: android android-recyclerview android-constraintlayout gridlayoutmanager

我有一个带有两种不同类型视图的RecyclerAdapter和一个具有15列的GridLayoutManager。在第一行中,有一个使用9个网格的type1视图,然后是两个每个使用3个网格的type2视图。在第二行中,我有两个type3的五个视图,每个视图使用3个网格。模式从那里重复。

enter image description here

问题是这样的:如果一行将type1的视图与type2的视图组合在一起,则type1的视图将以更大的高度增长,而type2的视图在底部保留一个空格。我想根据其他视图的高度调整第一个视图的高度。我不能使用固定的高度,第一个视图应该相对于其他视图进行调整。

type1布局:

 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_list_item_subtitle">

    <ImageView
        android:id="@+id/cover"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.5"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintLeft_toRightOf="@id/cover"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:id="@+id/title"
            style="title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/category_title"
            android:textColor="@color/white"/>

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_list_item_subtitle"
            android:textColor="@color/white"/>

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

type2布局:

 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/cover"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/cover"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="@dimen/category_title"
        android:textColor="@color/white"
        android:maxLines="1"
        android:ellipsize="end"/>

</androidx.constraintlayout.widget.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

我将type1的布局更改为这种布局,现在它的拉伸程度仅与其兄弟姐妹一样高。

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/recommendations_tracks_item_background">

    <ImageView
        android:id="@+id/cover"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="1"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintLeft_toRightOf="@id/cover"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <TextView
            android:id="@+id/title"
            style="title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/category_title"
            android:textColor="@color/white"
            android:gravity="center"
            android:maxLines="1"
            android:ellipsize="end"/>

        <TextView
            android:id="@+id/subtitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_list_item_subtitle"
            android:textColor="@color/white"
            android:gravity="center"
            android:maxLines="1"
            android:ellipsize="end"/>

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>