首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改GridLayoutManager布局

更改GridLayoutManager布局
EN

Stack Overflow用户
提问于 2020-04-10 20:40:14
回答 2查看 139关注 0票数 0

我想达到的目标是:

我所做的:

我有一个有1到5列的按钮,当用户循环在他想要的列数上时,布局会改变,但是所有的卡都粘在一起了。到目前为止,这就是我所能做的:https://imgur.com/a/CL4FZhY

我的项目

代码语言:javascript
复制
<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。是否需要为每个列屏幕创建一个布局?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-11 11:11:07

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

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

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

可能会有一些改进,以使任何帮助受到赞赏:)

票数 0
EN

Stack Overflow用户

发布于 2020-04-10 21:24:40

可以通过动态更改列表项根的边距来实现这一点,需要在适配器onBindViewHolder()函数中这样做。

代码语言:javascript
复制
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的函数

代码语言:javascript
复制
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;
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61148348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档