Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add space to the end of the RecyclerView

I have a recyclerview with a gridlayout. What I want is when the user scrolls to the end of the list (see my bad mockup), there should be an empty space with a height of 50dp, which isn't the same dimensions as my grid.

Note that this space is only visible at the very end end, as I do not want to change the layout. I could make it so that the recycerview has a margin bottom of 50dp, but I do not want to do that.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:fab="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent">      <android.support.v7.widget.RecyclerView         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         android:id="@+id/recyclerView"         android:layout_width="match_parent"         android:layout_height="match_parent"         tools:context=".MainActivity"         android:scrollbars="vertical"         />  </RelativeLayout> 
like image 961
HaloMediaz Avatar asked Apr 18 '15 16:04

HaloMediaz


People also ask

What is expandable RecyclerView?

Expandable RecyclerView is one of the most important feature in Android which can be easily created for our application . It contains two views one is parent view and other is child view. Parent is visible by default but the child view has to be expanded and collapsed. It will expand when we click on parent view.

How do I remove spaces between items in RecyclerView?

Try to use cardElevation=0dp. This should remove the extra spacing between recyclerview items.


2 Answers

<RecyclerView     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="50dp"     android:clipToPadding="false"     /> 
like image 193
mbmc Avatar answered Oct 05 '22 18:10

mbmc


This is best achieved with an item decoration.

Here's an example that works with a LinearLayoutManager - you'll have to adjust to suit for your Grid layout. What it does is checks each item to see if it's the last one, and if it is it adds the offset to the bottom of it. For a Grid layout, the hard part is figuring out whether your item position is in the last row or not.

// After setting layout manager, adapter, etc... float offsetPx = getResources().getDimension(R.dimen.bottom_offset_dp); BottomOffsetDecoration bottomOffsetDecoration = new BottomOffsetDecoration((int) offsetPx); mRecyclerView.addItemDecoration(bottomOffsetDecoration);  ...  static class BottomOffsetDecoration extends RecyclerView.ItemDecoration {     private int mBottomOffset;      public BottomOffsetDecoration(int bottomOffset) {         mBottomOffset = bottomOffset;     }      @Override     public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {         super.getItemOffsets(outRect, view, parent, state);         int dataSize = state.getItemCount();         int position = parent.getChildAdapterPosition(view);         if (dataSize > 0 && position == dataSize - 1) {             outRect.set(0, 0, 0, mBottomOffset);         } else {             outRect.set(0, 0, 0, 0);         }      } } 

For a GridLayoutManager, inside the getItemOffsets method you could do something similar to this to figure out if it's the last row:

GridLayoutManager grid = (GridLayoutManager)parent.getLayoutManager(); if ((dataSize - position) <= grid.getSpanCount()) {     outRect.set(0, 0, 0, mBottomOffset); } else {     outRect.set(0, 0, 0, 0); } 
like image 20
Adam S Avatar answered Oct 05 '22 17:10

Adam S