简体   繁体   English

Android - RecyclerView Grid对齐开始和结束但居中并且每行之间的空间相同

[英]Android - RecyclerView Grid aligned Start and end but centered and same space between each row

Im trying to achieve a recyclerView with a gridLayout with 3 columns and 2 rows.我正在尝试使用具有 3 列和 2 行的 gridLayout 来实现 recyclerView。

i need the first column aligned to the start parent and the third row aligned to the end of the parent.我需要第一列与起始父级对齐,第三行与父级对齐。 Of course the second column has to be centered in the parent.当然,第二列必须以父级为中心。

How can i achieve this?我怎样才能做到这一点?

在此处输入图像描述

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewAccessHome"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="28dp"
            app:layout_constraintTop_toBottomOf="@id/textViewAccesos"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:listitem="@layout/cell_accesos_menu_home"
            app:layoutManager="GridLayoutManager"
            app:spanCount="3"
            tools:itemCount="6"
            android:clipToPadding="false"/>

And the cell和细胞

<com.google.android.material.card.MaterialCardView
android:id="@+id/card_menu_access_home"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="90dp"
android:layout_height="100dp"
android:paddingHorizontal="15dp"
android:paddingVertical="17dp"
android:layout_marginBottom="15dp">

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imageViewCellRWHAccessHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic__close"/>

    <TextView
        android:id="@+id/textViewCellRWHAccessHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text here"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/roboto_medium"
        android:lines="2"
        android:textSize="12sp"
        android:textAlignment="center"/>

</androidx.appcompat.widget.LinearLayoutCompat>

</com.google.android.material.card.MaterialCardView>

I achieved it with this code, idk if it is correct.我用这段代码实现了它,如果它是正确的,idk。

recyclerViewAccessHome.addItemDecoration(HomeItemDecorator())

class HomeItemDecorator : RecyclerView.ItemDecoration() {

private var space = 22

override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) {

    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;
    outRect.top = space;

    if(parent.getChildAdapterPosition(view) == 2 || parent.getChildAdapterPosition(view) == 5) {
        outRect.right = 0;
    }

    if(parent.getChildAdapterPosition(view) == 0 || parent.getChildAdapterPosition(view) == 3) {
        outRect.left = 0;
    }

    if(parent.getChildAdapterPosition(view) == 1 || parent.getChildAdapterPosition(view) ==4) {
        outRect.left = space / 2;
        outRect.right = space / 2;
    }

}

} }

更新您的项目视图

android:layout_width="match_parent"

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

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