FerDensetsu
FerDensetsu

Reputation: 746

How to make an item's height in GridLayoutManager adjust relatively to another item?

I have a RecyclerAdapter with two different types of views and a GridLayoutManager with 15 columns. In the first row there's one view of type1 that uses 9 grids, followed by two views of type2 that use 3 grids each. In the second row I have five views of type2 that use 3 grids each; the pattern repeats from there.

enter image description here

The problem is this: if one row combines a view of type1 with views of type2, the view of type1 grows with a bigger height, leaving the views of type2 with a space at the bottom. I would like to adjust the height of the first view depending on what height the other views have. I can't use fixed heights, the first view should adjust relatively to the others.

type1 layout:

 <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 layout:

 <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>

Upvotes: 2

Views: 1513

Answers (1)

FerDensetsu
FerDensetsu

Reputation: 746

I changed my type1 layout to this and now it stretches only as high as its siblings.

    <?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>

Upvotes: 3

Related Questions