在 RecyclerView 的底部添加空白 space
Add blank space at the bottom of a RecyclerView
并且应用程序有一个项目列表(回收站视图),其大小不断增加。它的底部还有一个按钮,如果列表足够长,它会挡住列表。因此,我希望列表底部有一个空白 space,以便用户可以一直向下滚动,这样按钮只会挡住空白 space。可能不太容易想象我要做什么,所以这里有一些图片:
我要的是这个:
到目前为止,我想出的唯一解决方案是向回收站视图添加填充。但问题在于,当用户一直向下滚动时,我只希望空白 space 出现在列表底部。如果我在回收站视图中添加填充,即使列表下方还有项目,空白 space 也会存在。
所以我有这个:
我也尝试在底部添加一个“Space”对象,但它实际上并没有占用任何space(我猜android只使用“Space" 在对象之间插入 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>
您需要将 clipToPadding
设置为 false
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="72dp"
android:clipToPadding="false"/>
并且应用程序有一个项目列表(回收站视图),其大小不断增加。它的底部还有一个按钮,如果列表足够长,它会挡住列表。因此,我希望列表底部有一个空白 space,以便用户可以一直向下滚动,这样按钮只会挡住空白 space。可能不太容易想象我要做什么,所以这里有一些图片:
我要的是这个:
到目前为止,我想出的唯一解决方案是向回收站视图添加填充。但问题在于,当用户一直向下滚动时,我只希望空白 space 出现在列表底部。如果我在回收站视图中添加填充,即使列表下方还有项目,空白 space 也会存在。
所以我有这个:
我也尝试在底部添加一个“Space”对象,但它实际上并没有占用任何space(我猜android只使用“Space" 在对象之间插入 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>
您需要将 clipToPadding
设置为 false
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="72dp"
android:clipToPadding="false"/>