首页 > 解决方案 > 如何在 RecyclerView 中的最后一项之后设置恒定边距?

问题描述

在此处输入图像描述

last item我希望在RecyclerView图像之后有一些空间。

图像不是项目数少的状态,而是大到可以滚动的状态。

换句话说,当滚动到底部时,我希望最后一个项目位于图像的黑条上方。

但我已经实现的是:

在此处输入图像描述

正如您在图像中看到的那样,我已经实现的是该项目穿透条形并叠加在底部。

滚动时,栏是否与项目重叠并不重要,但是当滚动到底部时,我希望最后一个项目位于栏的顶部。

添加:

这些回收器视图动态地添加和删除项目。

一次,您只需发布 XML 代码。

如果您需要其他代码,请告诉我。

XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="24dp"
            android:background="@color/transparentBg"
            app:layout_constraintBottom_toBottomOf="parent">
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/add_exercise"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="8dp">

                <ImageView
                    android:id="@+id/add_icon"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:src="@drawable/ic_add"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:tint="@color/black" />

                <TextView
                    android:id="@+id/add_text"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="ADD ROUTINE"
                    android:textAlignment="center"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/add_icon" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </FrameLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

标签: androidandroid-recyclerview

解决方案


您可以将 FrameLayout 设置为特定高度,然后将您的 recyclerview 边距底部设置为与框架布局高度相同。

或者您可以将您的 ConstraintLayout 更改为 LinearLayout。


推荐阅读