【问题标题】:How to make an item's height in GridLayoutManager adjust relatively to another item?如何使 GridLayoutManager 中项目的高度相对于另一个项目进行调整?
【发布时间】:2019-09-12 23:25:15
【问题描述】:

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

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

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>

【问题讨论】:

    标签: android android-recyclerview android-constraintlayout gridlayoutmanager


    【解决方案1】:

    我将我的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多