ybybyb
ybybyb

Reputation: 1739

How to set constant margin after last item in RecyclerView?

enter image description here

I want there to be some space after the last item in RecyclerView like the image.

The image is not in a state where the number of items is small, but in a state that is large enough to be scrollable.

In other words, when scrolling to the bottom, I want the last item to be positioned above the black bar of the image.

But what I have implemented is:

enter image description here

What I have implemented, as you can see in the image, is that the item penetrates the bar and is superimposed on the bottom.

When scrolling, it doesn't matter if the bar overlaps the item, but when scrolling to the bottom, I want the last item to be on top of the bar.

ADDITION:

These recycler views dynamically add and delete items.

For once you just post the XML code.

If you need other code, please tell me.

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>

Upvotes: 1

Views: 1205

Answers (2)

Bobby Anggunawan
Bobby Anggunawan

Reputation: 44

You can set your FrameLayout to a specific height and then give your recyclerview margin bottom same as frame layout height.

Or you can just change your ConstraintLayout into a LinearLayout.

Upvotes: 1

Amin
Amin

Reputation: 3186

If you want your items to be displayed beneath the add Icon (which I personally do not like) and add some extra space in the bottom you can

  1. Add an empty view to the end of your adapter

  1. set clipToPadding=false and add a bottom padding which make the recycler view to skip that amount of height from bottom, but still show them (not clipping them) like this (I'm not sure if 160dp is a good number sum up your dimensions)
<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintTop_toTopOf="parent" 
            android:clipToPadding="false"
            android:paddingBottom="160dp"/>


  1. And if you want your recycler to be shown above the add button (which I think looks better) you can change it like this
<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/button" />

Upvotes: 1

Related Questions