Android RecyclerView GridLayoutManager 中心项并保持相同的宽度

问题描述

我将此 RecyclerView 配置为使用布局管理器:

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

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

android:layout_width="match_parent"

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

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

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

enter image description here

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

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

enter image description here

有什么办法可以用 GridLayoutManager 甚至 FlexBoxLayoutManager 做到这一点?

解决方法

您可以通过在 LinearLayout 中使用 GridLayout 和一些 <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>

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

The final result