android - 使用 GridLayoutManager 时 RecyclerView 项目 ConstraintLayout 困惑

标签 android android-recyclerview android-constraintlayout gridlayoutmanager

我在 RecyclerView 中使用 GridLayoutManagerspanCount 为 2。 以下是 RecyclerView 项的布局。它是一个包裹在 CardView 中的 ConstraintLayout:

<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 增加项目布局的高度时,两个图标都保持在底部。

谢谢

最佳答案

好的,经过一些点击和尝试,并进一步考虑 @Pawel Laskowski 的回答,以下是解决问题的方法:

  1. app:layout_constraintVertical_bias="1" 属性添加到 iv_locked
  2. ConstraintLayoutandroid:layout_heightwrap_content 更改为 match_parent

ConstraintLayout 很容易上手,但很难掌握。感谢@Pawel Laskowski 的帮助。

关于android - 使用 GridLayoutManager 时 RecyclerView 项目 ConstraintLayout 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53236799/

相关文章:

android - 单线程 OpenGL ES Android 应用程序

android - 测试未能运行完成。使用 Espresso 时的原因 : 'Instrumentation run failed due to ' Process crashed.

Android 动画 recyclerview

android - Stfalcon Chatkit 库

java - 如何使 RecyclerView 不可点击并将 onClick 事件传递给父 View ?

Android ConstraintLayout TextView 跳出屏幕

android - 如何使用 ActivityRecognition API 检测转换中的变化?

android - 避免再次启动应用程序

android - 如何将 Webview 放在其他 View 下方并将所有 View 滚动到一起

android - 添加圆角到约束布局会留下白色背景