更改GridLayoutManager的布局

问题描述 投票:0回答:1

我想要实现的目标:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS85d0JaMWwucG5nIn0=” alt =“在此处输入图像描述”>“ >>

我做了什么:

我有一个具有1至5列的按钮,当用户选择所需的列数时,布局会发生变化,但所有卡都彼此粘在一起。到目前为止,这是我已经能够做到的:https://imgur.com/a/CL4FZhY

我的项目

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/video_preview"
        android:layout_width="match_parent"
        android:layout_height="194dp"
        android:scaleType="centerCrop"
        tools:src="@drawable/img_error_v1"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:id="@+id/video_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?attr/textAppearanceHeadline6"
            android:maxLines="1"
            tools:text="This is a really really really really really really really really really really really long title"
            />
        <TextView
            android:id="@+id/video_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:maxLines="1"
            android:textAppearance="?attr/textAppearanceBody2"
            android:textColor="?android:attr/textColorSecondary"
            tools:text="This is the URL for now"
            />
    </LinearLayout>
</LinearLayout>

我的问题:

我如何实现这种转变?我已经读过有关ItemDecoration的信息,但我认为我无法更改我的商品的layout_height。我是否需要为每个列屏幕创建一个布局?

我想要实现的目标:我做了什么:我有一个1到5列的按钮,当用户确定他想要的列数时,布局会发生变化,但是所有卡都彼此粘在一起。到目前为止...

android android-viewholder gridlayoutmanager
1个回答
0
投票

您可以通过动态更改列表项的根的边距来实现,您需要在适配器onBindViewHolder()函数中执行此操作

© www.soinside.com 2019 - 2024. All rights reserved.