android - 如何在 Android RecyclerView - GridLayoutManager 中将列设置在行的中心

标签 android android-recyclerview kotlin gridlayoutmanager

我有 RecyclerViewGrid layout。一切正常。 但我需要将列设置在水平中心。例如。我有 18 个项目,跨度数为 4。对于最后一行,我有 2 个项目。这两个项目应该在水平中心而不是左边。

这是我的RecyclerView:

 <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_option"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:overScrollMode="never"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        android:layout_gravity="center_horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rv_answer" />

这是我的适配器布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_marginStart="@dimen/marginVSmall"
    android:layout_marginEnd="@dimen/marginVSmall"
    android:layout_marginTop="@dimen/marginVSmall"
    android:layout_marginBottom="@dimen/marginVSmall"
    android:layout_height="wrap_content">

    <curvegraph.com.vijay.widgets.SquareTextView
        android:id="@+id/tv_option"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:gravity="center"
        android:text="A"
        android:textStyle="bold"
        android:maxLength="1"
        android:textColor="@color/colorWhite"
        android:textSize="@dimen/textXLarge"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />
</android.support.constraint.ConstraintLayout>

我的 Kotlin 代码来设置 LayoutManager

rv_option.layoutManager = GridLayoutManager(this, 4)

这是我的 Kotlin 代码:

 val grid =   GridLayoutManager(this, 8)
        grid.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {

            override fun getSpanSize(position: Int): Int {
                return if (position > 15) { // totalRowCount : How many item you want to show
                    2 // the item in position now takes up 4 spans
                } else 1
            }
        }
        rv_option.layoutManager=grid
        rv_option.setHasFixedSize(true)
        optionsAdapter.spanCountFun(8)

最佳答案

试试这个希望这会有所帮助。

val gridLayoutManager = GridLayoutManager(this, 4)
    gridLayoutManager.setSpanSizeLookup(object : GridLayoutManager.SpanSizeLookup() {

        override fun getSpanSize(position: Int): Int {
            return if (position == 16) { // totalRowCount : How many item you want to show
                4 // the item in position now takes up 4 spans
            } else 1
        }
    })

    recyclerView.layoutManager = gridLayoutManager
    recyclerView.setHasFixedSize(true)

我已经有了自己的答案:https://stackoverflow.com/a/50247597/5167909

关于android - 如何在 Android RecyclerView - GridLayoutManager 中将列设置在行的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571002/

相关文章:

java - 一些 Recyclerview 项目相互影响

performance - Kotlin:大量 ConsPStack,我该如何避免?

java - 如何在android中以编程方式设置listview高度

java - 一项 Activity 中有 2 个 Action

android - 在Android的kotlin类中遇到多个错误?

android - 如何从 Android Google Play 中删除测试 IAP 购买

android - 如何使用 Android 中的歌曲自动执行 Seekbar 移动?

android - 在运行时获取 RecyclerView subview 的高度

android - 无法在 Android Studio 3.3 Beta 2 中下载 groovy-all.jar (org.codehaus.groovy :groovy-all:2. 4.15)

android - 是否可以在一个 Java 文件中混合 Java 代码和 Kotlin 代码