Margin / padding in the last child of a RecyclerView

I am trying to add Padding / Margin Bottom in the last row and Padding / Margin Top in the front row. I can not do this in the xml element, as this will affect all my children.

The RecyclerView adapter has headers and children, so I can't use

android:padding="4dp" android:clipToPadding="false" 

I need to use it separately in the last first line of each header

+92
android padding android-recyclerview
Nov 17 '15 at 10:20
source share
8 answers

This problem is even easier to solve. You can apply the necessary addition to the RecylerView itself and set clipToPadding to false, otherwise indents cut off your scroll area. Here is an example

 <android.support.v7.widget.RecyclerView android:padding="4dp" android:clipToPadding="false" android:layout_width="match_parent" android:layout_height="match_parent" /> 

See addition will add 4dp from all sides, including the top and bottom. The clipToPadding parameter then ensures that your children are not chopped off. Now add a 4dp addition to all sides for your children, and you're good to go. In total, you get an 8dp supplement on the sides and between points.

+185
Nov 17 '15 at 10:29
source share

Instead of indenting the top and bottom elements, you can simply add an addition to the top and bottom of your RecyclerView and set the clipToPadding attribute to false .

 <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:paddingTop="8dp" android:paddingBottom="8dp" /> 
+64
Nov 17 '15 at 10:28
source share

use ItemDecoration :

 private class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int position = parent.getChildAdapterPosition(view); boolean isLast = position == state.getItemCount()-1; if(isLast){ outRect.bottom = space; outRect.top = 0; //don't forget about recycling... } if(position == 0){ outRect.top = space; // don't recycle bottom if first item is also last // should keep bottom padding set above if(!isLast) outRect.bottom = 0; } } } 

and

 //8dp as px int space = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics()); // calculated //int space = getResources().getDimensionPixelSize( // R.dimen.list_item_padding_vertical); // from resources recyclerView.addItemDecoration(new SpacesItemDecoration(space)); 
+33
Nov 17 '15 at 10:26
source share
  <android.support.v7.widget.RecyclerView android:id="@+id/rv_tpf" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:paddingBottom="100dp" /> 

Add android:clipToPadding="false" and android:paddingBottom="100dp" in your android:paddingBottom="100dp" processor.

+13
Sep 18 '18 at 10:43
source share

For some reason, the old clipToPadding=false solution doesn't work for me. So I added ItemDecoration

https://gist.github.com/kassim/582888fa5960791264fc92bc41fb6bcf

 public class BottomPaddingDecoration extends RecyclerView.ItemDecoration { private final int bottomPadding; public BottomPaddingDecoration(int bottomPadding) { this.bottomPadding = bottomPadding; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition(); if (position == parent.getAdapter().getItemCount() - 1) { outRect.set(0, 0, 0, bottomPadding); } } } 
+3
Sep 13 '17 at 16:01
source share

I modified the amazing @snachmsm answer to better understand how to use it correctly.

 public class SpacesItemDecoration extends DividerItemDecoration { private int space; public SpacesItemDecoration(Context clContext,int oriantation,int space) { super(clContext,oriantation); this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { super.getItemOffsets(outRect,view,parent,state); int position = parent.getChildAdapterPosition(view); boolean isLast = position == state.getItemCount()-1; if(isLast){ outRect.bottom = space; outRect.top = 0; //don't forget about recycling... } /* if(position == 0){ outRect.top = space; // don't recycle bottom if first item is also last // should keep bottom padding set above if(!isLast) outRect.bottom = 0; }*/ } } 
+1
Jan 11 '19 at 13:44
source share

Add android: clipToPadding = "false" and android: paddingBottom = "65dp" in your processor review. If you use the awesome menu button and actions in the processor view cell.

 <androidx.recyclerview.widget.RecyclerView android:id="@+id/dinner_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:clipToPadding="false" android:paddingBottom="65dp"/> 
+1
Jul 02 '19 at 10:39 on
source share

I use this in kotlin

 override fun onBindViewHolder(holder: RecyclerView.ViewHolder(view), position: Int) { if (position == itemsList.lastIndex){ val params = holder.itemView.layoutParams as FrameLayout.LayoutParams params.bottomMargin = 100 holder.itemView.layoutParams = params }else{ val params = holder.itemView.layoutParams as RecyclerView.LayoutParams params.bottomMargin = 0 holder.itemView.layoutParams = params } //other codes ... } 
0
Dec 09 '18 at 9:38
source share



All Articles