网格布局的 Recyclerview 动态高度

Recycerview dynamic height for grid layout

我想在回收站视图中实现这种网格布局的动态高度。如果一个单元格有两行,那么同一行中的其他单元格应该具有相同的高度,尽管其他单元格中有一行。

这是我的行文件代码:

<?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="wrap_content"
    android:layout_marginRight="-4dp"
    android:layout_marginTop="-4dp"
    android:layout_marginBottom="-20dp"
    android:layout_marginLeft="-12dp">

    <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/cvHomeItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="@dimen/margin_8"
        app:cardElevation="@dimen/margin_8"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:id="@+id/llChild"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_6ssp"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/minus_margin_2"
                android:gravity="center_vertical"
                android:weightSum="1">

                <ImageView
                    android:id="@+id/ivImage"
                    android:layout_width="@dimen/margin_24"
                    android:layout_height="@dimen/margin_24"
                    android:layout_marginLeft="@dimen/margin_2"
                    android:scaleType="centerInside"
                    tools:src="@drawable/ic_announcement"
                    android:contentDescription="@string/app_name" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="end">

                    <TextView
                        android:id="@+id/tvDays"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/margin_12"
                        android:gravity="center_vertical"
                        android:lines="2"
                        android:textColor="@color/colorText"
                        android:textSize="@dimen/_11ssp"
                        tools:text="3.5 Day(s)" />
                </LinearLayout>
            </LinearLayout>

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Annual Leave"
                android:textColor="@color/colorText"
                android:textSize="@dimen/_11ssp" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

如果需要任何说明或任何代码,请告诉我。谢谢!

试试这个

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvItemMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical"
app:cardCornerRadius="6dp"
app:cardElevation="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/ivLogo"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:padding="6dp"
        android:src="@drawable/ic_your_image"
        app:layout_constraintBottom_toTopOf="@id/tvName"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:gravity="bottom"
        android:maxLines="2"
        android:minLines="2"
        android:text="Text 1"
        android:textColor="#000"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@id/tvPrice"
        app:layout_constraintStart_toStartOf="@id/ivLogo"
        app:layout_constraintTop_toBottomOf="@id/ivLogo"
        app:layout_constraintWidth_percent="0.8" />

    <TextView

        android:id="@+id/tvPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:maxLines="1"
        android:minLines="1"
        android:text="Text 2"
        android:textColor="#F56114"
        android:textSize="14sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@id/tvName"
        app:layout_constraintTop_toBottomOf="@id/tvName" />


</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

将线性布局 ("@+id/llChild) 的高度设置为 match_parent

<?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="wrap_content"
    android:layout_marginRight="-4dp"
    android:layout_marginTop="-4dp"
    android:layout_marginBottom="-20dp"
    android:layout_marginLeft="-12dp">

    <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/cvHomeItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="@dimen/margin_8"
        app:cardElevation="@dimen/margin_8"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:id="@+id/llChild"
            android:layout_width="match_parent"
            android:layout_height="match_parent" <= correction
            android:layout_margin="@dimen/_6ssp"
            android:gravity="center_vertical"
            android:orientation="vertical">

            .....

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>