简体   繁体   English

如何在 Android 上的 RecyclerView 中使用 GridLayoutManager

[英]How to use GridLayoutManager in RecyclerView on Android

In my application I have recyclerView and for this I should use GridLayoutManager and set 2 items per column.在我的应用程序中,我有recyclerView ,为此我应该使用GridLayoutManager并为每列设置 2 个项目。

I want set this 2 items fit of screen, each items sticky to screen.我想设置这 2 个项目适合屏幕,每个项目都粘在屏幕上。 Such as below image:比如下图:

在此处输入图像描述

I write below codes, but after run application show me such as this:我写了下面的代码,但在运行应用程序后显示如下:

在此处输入图像描述

And I want set 120dp for layout_width and layout_height !我想为layout_widthlayout_height设置120dp

My XML code:我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rowMasterQuestion_root"
    android:layout_width="@dimen/_120mdp"
    android:layout_height="@dimen/_120mdp"
    android:background="@drawable/gray_border">

    <TextView
        android:id="@+id/rowMasterQuestion_title"
        android:layout_width="@dimen/_120mdp"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_15mdp"
        android:gravity="center_horizontal"
        android:textColor="@color/black"
        android:textSize="@dimen/_9font_mdp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rowMasterQuestion_answer"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/_15mdp"
        android:layout_marginLeft="@dimen/_10mdp"
        android:layout_marginRight="@dimen/_10mdp"
        android:layout_marginBottom="@dimen/_20mdp"
        android:gravity="center"
        android:paddingLeft="@dimen/_5mdp"
        android:paddingRight="@dimen/_5mdp"
        android:textColor="@color/white"
        android:textSize="@dimen/_8font_mdp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

My Activity codes:我的活动代码:

requestMain_childList.apply {
            layoutManager = GridLayoutManager(this@RequestMainPage, 2, GridLayoutManager.VERTICAL, false)
            hasFixedSize()
            adapter = masterChildAdapter
}

How can I fix this issue?我该如何解决这个问题?

This is tricky because you are wanting a different layout gravity for each item, depending on the column.这很棘手,因为您希望每个项目具有不同的布局重力,具体取决于列。 I think that will have to be set in the RecyclerView.Adapter.onBindViewHolder function.我认为必须在RecyclerView.Adapter.onBindViewHolder function 中进行设置。

First, wrap the ConstraintLayout of your item view in a FrameLayout.首先,将项目视图的 ConstraintLayout 包装在 FrameLayout 中。 Make the width of the outer FrameLayout match_parent so it will fill its column in the RecyclerView.使外部 FrameLayout 的宽度为match_parent ,以便它将填充其在 RecyclerView 中的列。 You can also give the FrameLayout the amount of start/end padding you want to hold the items away from the sides of the screen.您还可以为 FrameLayout 提供您希望将项目远离屏幕两侧的开始/结束填充量。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="16dp"
    android:paddingEnd="16dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/wrappedView"
        android:layout_width="@dimen/_120mdp"
        android:layout_height="@dimen/_120mdp"
        ...>

    ...

    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

Then in onBindViewHolder , you can set the gravity of the inner view to the left or right depending on which column it is in (even or odd data position).然后在onBindViewHolder中,您可以根据它所在的列(偶数或奇数数据位置)将内部视图的重力设置为向左或向右。

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        // ...
        val innerView = holder.wrappedView // or however you have it stored in view holder
        (innerView.layoutParams as FrameLayout.LayoutParams).gravity = Gravity.CENTER_VERTICAL or 
                (if (position % 2 == 0) Gravity.START else Gravity.END)
    }

Note, if you want the items to be centered in their columns instead, this is much easier.请注意,如果您希望项目在它们的列中居中,这要容易得多。 You can just set the gravity of the inner view to center in the XML and you don't have to do anything in the Adapter.您可以在 XML 中将内部视图的重心设置为center ,而无需在适配器中执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM