birgersp
birgersp

Reputation: 4926

Add blank space at the bottom of a RecyclerView

And app has a list of items (recycler view) that grows in size. It also has a button at the bottom, which will obstruct the list if the list is long enough. Therefore, I want there to be a blank space at the bottom of the list, so that the user can scroll all the way down so that the button only obstructs the blank space. Probably not very easy to envision what I'm trying to do, so here are some pictures:

short list

long list

What I want is this:

solution

So far, the only solution I've come up with is add padding to the recycler view. But the problem with that is that I only want the blank space to appear at the bottom of the list when the user scrolls all the way down. If I add padding to the recycler view, the blank space is there even when there are items further down the list.

So what I have is this:

hidden items

I also tried adding a "Space" object at the bottom, but it doesn't actually take up any space (I guess the android only uses "Space" to insert space between objects and not when there is no object separation).

Here is my 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>

Upvotes: 4

Views: 1985

Answers (1)

GSala
GSala

Reputation: 986

You need to set clipToPadding to false

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

Upvotes: 14

Related Questions