0

我的交错网格布局列空间发生了变化(正如您在第二行中看到的那样,两个项目之间的差距增加了一点。

这是用于在项目之间添加空间的 EqualSpacingGridItemDecoration 类:

class EqualSpacingGridItemDecoration @JvmOverloads constructor(private val spacing: Int) :
    RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildViewHolder(view).adapterPosition
        outRect.left = if (position % 2 == 0) spacing else spacing / 2
        outRect.right = if (position % 2 == 1) spacing else spacing / 2
        outRect.top = if (position == 0 || position == 1) 0 else spacing

        outRect.bottom = 0
    }
}

这是单个项目的 XML:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/layoutTask"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="@dimen/_6sdp"
        app:cardElevation="0dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_6sdp"
                android:layout_marginEnd="@dimen/_6sdp"
                android:background="@drawable/bg_rounded_textview"
                android:drawableStart="@drawable/ic_globe"
                android:drawablePadding="@dimen/_4sdp"
                android:gravity="center"
                android:padding="@dimen/_5sdp"
                android:text="10 MB"
                android:textSize="@dimen/_9ssp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <com.airbnb.lottie.LottieAnimationView
                android:id="@+id/viewAnimation"
                android:layout_width="@dimen/_60sdp"
                android:layout_height="@dimen/_60sdp"
                android:layout_marginTop="@dimen/_6sdp"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvData"
                app:lottie_autoPlay="true"
                app:lottie_loop="true" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvTitle"
                style="@style/SemiBoldTextStyle2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="@dimen/_19sdp"
                android:layout_marginTop="@dimen/_6sdp"
                android:gravity="center_horizontal"
                android:maxWidth="@dimen/_130sdp"
                android:text="Run 10,000 Steps"
                android:textSize="@dimen/_11ssp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/viewAnimation" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvDescription"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="@dimen/_9sdp"
                android:layout_marginTop="@dimen/_3sdp"
                android:gravity="center_horizontal"
                android:maxWidth="@dimen/_130sdp"
                android:textSize="@dimen/_9ssp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvTitle"
                tools:text="Run or walk, complete 10,000 steps today and claim your reward" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvTimer"
                style="@style/SemiBoldTextStyle2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_8sdp"
                android:background="@drawable/bg_rounded_textview_with_border"
                android:drawablePadding="@dimen/_5sdp"
                android:paddingHorizontal="@dimen/_7sdp"
                android:paddingVertical="@dimen/_5sdp"
                android:text="6h : 25m : 16s"
                android:textSize="@dimen/_8ssp"
                android:visibility="gone"
                app:drawableStartCompat="@drawable/ic_timer"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvDescription" />

            <ProgressBar
                android:id="@+id/viewLinearProgress"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="4dp"
                android:layout_marginHorizontal="@dimen/_18sdp"
                android:layout_marginTop="@dimen/_12sdp"
                android:indeterminate="false"
                android:progress="60"
                android:progressDrawable="@drawable/gradient_progressbar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvTimer" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_3sdp"
                android:background="@drawable/bg_rounded_textview2"
                android:gravity="center"
                android:minWidth="@dimen/_60sdp"
                android:paddingVertical="@dimen/_3sdp"
                android:text="1/3"
                android:textSize="@dimen/_8ssp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/viewLinearProgress" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnDoIt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="@dimen/_6sdp"
                android:layout_marginVertical="@dimen/_12sdp"
                android:backgroundTint="@color/light_orange2"
                android:insetTop="@dimen/_6sdp"
                android:insetBottom="@dimen/_6sdp"
                android:paddingVertical="@dimen/_5sdp"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/_11ssp"
                app:cornerRadius="@dimen/_6sdp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/tvProgress"
                app:strokeColor="@color/colorPrimary"
                app:strokeWidth="@dimen/_1sdp"
                tools:text="Let’s do it" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</layout>

这是我得到的视图: 交错网格布局视图

这里有什么问题?请任何人帮忙。

4

0 回答 0