【问题标题】:Change layout of GridLayoutManager更改 GridLayoutManager 的布局
【发布时间】:2020-04-10 20:40:14
【问题描述】:

我想要达到的目标:

我做了什么:

我有一个 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。我需要为每一列屏幕创建一个布局吗?

【问题讨论】:

  • 澄清一下,您希望您的每个项目仍然具有完整宽度,但现在每行 2 个?
  • 嗯,例如,在一列中我想要 100% 的宽度,对于 2 列 50% 可能会少一点以留出边距,3 列 33% 等等......高度会改变还有
  • 它可以为您提供帮助。 answer
  • 是的,我看到了ItemDecoration 示例,但问题是在我的一列中,图像的高度例如为 200dp,我希望 2 列的高度为 100dp

标签: android android-viewholder gridlayoutmanager


【解决方案1】:

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

GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.card.getLayoutParams();
            if (position % 2 == 0) {
                params.setMargins(dpTopixel(20), dpTopixel(10), dpTopixel(10), dpTopixel(5));
            } else {
                params.setMargins(dpTopixel(10), dpTopixel(10), dpTopixel(20), dpTopixel(5));
            }
            holder.card.setLayoutParams(params);

这是将像素转换为dp的函数

private int dpTopixel(int dp) {
    float dip = (float) dp;
    Resources r = context.getResources();
    float px = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dip,
            r.getDisplayMetrics()
    );
    return (int) px;
}

如果您在两个屏幕上使用相同的适配器,那么您可以使用适配器的构造函数发送一个标志来阻止该代码

【讨论】:

  • 我试过你说的,但它没有回答我的问题,因为我想在显示 2,3 列或更多列时更改卡片的高度,它还添加了边距到我所有的显示器:/
  • 其实你可以改变高度和宽度,就像 params.heightparams.width 一样,并且避免对你所有的人实施这个显示,您需要使用适配器构造函数发送一些内容,说明应该显示多少列,以便您可以随意操作它。
  • 但实际上,如果您需要的不仅仅是简单的更改,则需要使用多个 View Holder 将它们分开,例如 stackoverflow.com/questions/26245139/… 。我认为在您的情况下这是一种更好的方法
  • 是的,我实际上是在回答你,我试过 params.height 但它以一种奇怪的方式改变了我的卡片,我再也看不到文字了,我已经回答了这篇文章,但我的答案已被删除,我正在等待它再次弹出。我已经设法使用多个ViewHolderItemDecoration 来做到这一点。我不知道这是否是最好的方法,你可以查看 repo here
【解决方案2】:

我不知道这是否是正确的解决方案,但这个 article 帮助我根据列数更改视图的布局。

  • 缺点是我需要为要显示的每个列布局创建不同的布局。
  • 好处是我可以更好地控制每个布局的项目,并且可以修改文本大小或其他任何内容。

由于它有点长,我创建了一个repo 来展示我是如何做到的。

可能会有一些改进以提供任何帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多