RecyclerView элемент ConstraintLayout портится при использовании GridLayoutManager - PullRequest
0 голосов
/ 10 ноября 2018

Я использую GridLayoutManager в моем RecyclerView с spanCount из 2. Ниже приведен макет элемента RecyclerView. Это ConstraintLayout, завернутый в CardView:

<android.support.v7.widget.CardView
    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="wrap_content"
    android:layout_margin="@dimen/cardview_margin"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardBackgroundColor="@android:color/white"
    app:cardCornerRadius="4dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:text="Title"
            android:textAllCaps="true"
            android:textColor="@color/colorPrimary"
            android:textSize="16sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_sub_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Subtitle"
            android:textAllCaps="false"
            android:textSize="14sp"
            app:layout_constraintEnd_toEndOf="@+id/tv_title"
            app:layout_constraintStart_toStartOf="@+id/tv_title"
            app:layout_constraintTop_toBottomOf="@+id/tv_title" />

        <ImageView
            android:id="@+id/iv_favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:scaleX="1.07"
            android:scaleY="1.07"
            android:src="@drawable/ic_star_blank_grey"
            app:layout_constraintBottom_toBottomOf="@+id/iv_locked"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/iv_locked"/>

        <ImageView
            android:id="@+id/iv_locked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:scaleX="1.0"
            android:scaleY="1.0"
            android:src="@drawable/ic_lock_open_grey"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/iv_favorite"
            app:layout_constraintTop_toBottomOf="@+id/tv_sub_title" />
    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

Проблема возникает, когда GridLayoutManager имеет тенденцию увеличивать высоту элемента RecyclerView в соответствии с высотой элемента во втором столбце. Обе иконки ImageView выходят из родительского дна и имеют тенденцию придерживаться TextView tv_sub_title, как на скриншоте ниже:

Пожалуйста, дайте мне знать, как это исправить, чтобы обе иконки оставались внизу, когда высота макета элемента увеличивается на GridLayoutManager.

Спасибо

Ответы [ 2 ]

0 голосов
/ 10 ноября 2018

Хорошо, с некоторыми хитами и пробной версией @Pawel Laskowski ответим немного дальше, вот что решило проблему:

  1. Добавить атрибут app:layout_constraintVertical_bias="1" к iv_locked
  2. Изменение android:layout_height из ConstraintLayout с wrap_content на match_parent.

ConstraintLayout довольно легко начать, но довольно сложно освоить. Спасибо за помощь @Pawel Laskowski.

0 голосов
/ 10 ноября 2018

Чтобы удерживать iv_locked выровненным по низу между его вертикальными ограничениями, вам нужно установить максимальное вертикальное смещение, добавив атрибут app:layout_constraintVertical_bias="1" к этому. Поскольку iv_favorite ограничен по вертикали до iv_locked, он автоматически будет в соответствующей позиции.

...