java - 如何使 GridLayoutManager 的内容居中?

标签 java android xml android-recyclerview grid-layout

只是试图将我的 RecyclerView 水平居中。
这是xml:

<?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"
    tools:context=".MainActivity">



    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"/>


</RelativeLayout>
这里是java代码:
mRecyclerView = findViewById(R.id.recycler_view);
itemLayoutManager = new GridLayoutManager(this, 6);
mRecyclerView.setLayoutManager(itemLayoutManager);
...
mItemAdapter = new ItemAdapter(MainActivity.this, mItemList);
mRecyclerView.setAdapter(mItemAdapter);
当我运行应用程序时,recyclerview 左对齐。
我究竟需要做什么才能使内容居中,以使边距与右侧示例图片中的左右相同?
enter image description here

最佳答案

创建一个类,例如为项目设置填充:

class SpacingItemDecorator (private val padding: Int) : RecyclerView.ItemDecoration()
{
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    )
    {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.top = padding
        outRect.bottom = padding
        outRect.left = padding
        outRect.right = padding
    }
}
现在在您的 RecyclerView 集中 paddingStartpaddingEnd ,例如 4dp :
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="4dp"
    android:paddingEnd="4dp"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="6"/>
当您创建适配器时,只需应用此项目装饰器:
val x = (resources.displayMetrics.density * 4).toInt() //converting dp to pixels
recyclerView.addItemDecoration(SpacingItemDecorator(x)) //setting space between items in RecyclerView
回收站查看项目:
<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="6dp"
    app:cardElevation="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txtContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{Integer.toString(dataView.id)}"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
结果:
enter image description here

关于java - 如何使 GridLayoutManager 的内容居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63839194/

相关文章:

java - 当方法名与类名相同时如何实例化嵌套类?

java - 开发适用于智能手机和平板电脑的 Android 应用程序

android - PhoneGap 不从 HTTP 加载图像

java - 图片无法上传到 FTP

javascript - 如何访问中继器内的外部元素?

java - 当周围的一个限定时,私有(private)静态字段是否应该从嵌套类中可见?

java - 在方法中迭代数组 (Android)

android - 如何指定用于 Android 可穿戴设备的布局?

java - 添加了新的bean.xml并获取类路径资源[Spring-Mail.xml]无法打开,因为它不存在],其根本原因

java - 我来自 XML 的 VIEWS 或 WIDGETS 无法在扩展 fragment 类的类上使用 java