给RecyclerView底部添加空白间距

4

一个应用程序有一个项目列表(回收视图),其大小会增加。它还有一个按钮位于底部,如果列表足够长,将会遮挡列表。因此,我希望列表底部有一个空白区域,以便用户可以向下滚动到按钮只遮挡该空白区域。可能很难想象我试图做什么,因此这里有一些图片:

short list

long list

我想要的是这个:

solution

到目前为止,我想到的唯一解决方案就是给回收视图添加填充。但问题在于,我只希望在用户滚动到列表底部时才出现空白处。如果我给回收视图添加填充,即使列表中还有更多项,空白处也会存在。
我现在的情况如下:

hidden items

我也尝试在底部添加一个“Space”对象,但它实际上不占用任何空间(我猜Android仅在对象之间插入空格时使用“Space”,而不是在没有对象分隔符时使用)。
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainActivity">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout">

        <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:theme="?attr/actionBarTheme"
                android:minHeight="?attr/actionBarSize"
                android:id="@+id/toolbar" />

        <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/recyclerView"
                android:paddingBottom="72dp">

        </androidx.recyclerview.widget.RecyclerView>

    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:src="@drawable/plus_thick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:id="@+id/fab"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:contentDescription="Add item" />

</androidx.constraintlayout.widget.ConstraintLayout>

请参考此链接:https://dev59.com/eF0b5IYBdhLWcg3wA9Ab - Kunu
1个回答

11

您需要将clipToPadding设置为false

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="72dp"
            android:clipToPadding="false"/>

对于使用不同类型向列表添加视图的方法,这个方法是完美的答案。 - 卡尔斯路西法

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接