【问题标题】:Android RecyclerView GridLayoutManager center items and keep same widthAndroid RecyclerView GridLayoutManager 中心项目并保持相同的宽度
【发布时间】:2021-07-27 23:48:03
【问题描述】:

我将这个 RecyclerView 配置为布局管理器:

recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

我的具体情况是子项没有预定义的宽度。相反,我将它们的宽度设置为

android:layout_width="match_parent"

然后我希望 GridLayoutManager 为每个项目提供 1/3 的可用空间

到目前为止一切顺利,但我希望如果在最后一行中只有 1 或 2 个项目要显示,我想将它们居中。

我尝试了this 解决方案,但得到的是:

正如您在最后一行中看到的,项目被拉伸到布局的边缘,并且 parent 的宽度在它们之间划分。发生这种情况是因为正如我所说,它们的宽度没有预先定义。

我期望计算项目宽度,因为它们每行显示 3 个项目,然后将它们居中。如图所示:

我有什么办法可以用GridLayoutManager 甚至FlexboxLayoutManager 做这个吗?

【问题讨论】:

  • 我的回答对您有帮助吗? :)
  • @DEX7RA 不幸的是,这不是我的问题的解决方案。我希望这些物品彼此之间具有相同的尺寸和相同的距离。如果最后一行中的项目更居中并且与第一行中的项目保持相同的距离,您的解决方案将是可以接受的。感谢您的回答。

标签: android android-recyclerview gridlayoutmanager android-flexboxlayout


【解决方案1】:

您可以通过在GridLayout 中使用LinearLayout 和一些<Space 来实现该效果:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:ignore="MissingConstraints"
        android:layout_marginTop="20dp"
        android:id="@+id/l1">

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#000000" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#000000" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#000000" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>
    </LinearLayout>

    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:ignore="MissingConstraints"
        android:layout_below="@+id/l1"
        android:layout_marginTop="20dp"
        >

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#000000" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#000000" />

        <Space
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1">
        </Space>
    </LinearLayout>
</RelativeLayout>

这就是它在我的屏幕上的样子:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多