Margin/padding in last Child in RecyclerView

I'm trying to add Padding/Margin Bottom in the last row and Padding/Margin Top in the first row. I can not do it in the item xml as it would affect all of my Childs.

I have headers and childs in my RecyclerView Adapter so I can not use the

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

I need to use it individually on the last first row of each header

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 ...
}

android - Margin/padding in last Child in RecyclerView, I'm trying to add Padding/Margin Bottom in the last row and Padding/Margin Top in the first row. I can not do it in the item xml as it would affect all of my Childs. Questions: I several screens that use RecyclerView and also has a small Fragment that is on top of the RecyclerView. When that Fragment is displayed I’d like to make sure I can scroll to the bottom of the RecyclerView. The Fragment isn’t always displayed. If I used margins on the RecyclerView I’d need to dynamically

This issue is even easier to solve. You can apply necessary padding to the RecylerView itself and set clipToPadding to false, otherwise, the padding will chop off your scrolling 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 the padding will add 4dp on all sides including top and bottom. Then the clipToPadding parameter makes sure your child items are not chopped off. Now, add 4dp padding to all sides for your child items, and you are good to go. In total you get 8dp padding on sides and between items.

Right way of setting margin on Recycler View's cell, But some might miss out how to set the recycler view's cells' margin in the right way. android:layout_margin"@dimen/default_padding" Android Margin and Padding aren't working for RecyclerViews inside ScrollViews. Ask Question Asked 1 year, Margin/padding in last Child in RecyclerView. 1.

Instead of adding padding to both the top and bottom items, You can just add the padding 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" />

RecyclerView padding to first and last items · Taneli Korri, Adding padding to a RecyclerView might not turn out as you expected. top padding to the first element and bottom padding to the last item? android:drawableLeft margin and/or padding. Thanks for contributing an answer to Stack Overflow! Margin/padding in last Child in RecyclerView.

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));

Quick way to add padding to first and last item in recyclerview via , @param edgePadding padding set on the left side of the first item and the right side of the last item. */. public EdgeDecorator(int edgePadding)  public class RecyclerViewMargin extends RecyclerView.ItemDecoration { private final int columns; private int margin; /** * constructor * @param margin desirable margin size in px between the views in the recyclerView * @param columns number of columns of the RecyclerView */ public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0

 <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 recyclerview.

A RecyclerView.ItemDecoration that adds 8 dp padding to the top of , ItemDecoration that adds 8 dp padding to the top of the first and bottom of the last item in a list (or RecyclerView in this case). ListPaddingDecoration.java. public class RecyclerViewMargin extends RecyclerView.ItemDecoration { private final int columns; private int margin; /** * constructor * @param margin desirable margin size in px between the views in the recyclerView * @param columns number of columns of the RecyclerView */ public RecyclerViewMargin(@IntRange(from=0)int margin ,@IntRange(from=0

Margin/padding in last Child in RecyclerView, I'm trying to add Padding/Margin Bottom in the last row and Padding/Margin Top in the first row. I can not do it in the item xml as it would affect all of my Childs. Add bottom margin to last item in Adapter implementation. That should work, but for unknown reason I still can't get it work. I don't like this way, because I don't like to modify dimensions in code. So. There is already imaginary way – construct some XML drawable with particular bitmap and margin.

Margin/padding in last Child in RecyclerView, I'm trying to add Padding/Margin Bottom in the last row and Padding/Margin Top in the first row. I can not do it in the item xml as it would affect  @zatziky - yep, if you already use top and bottom padding as part of your RecyclerView (and use clipToPadding="false"), then you can restructure things slightly. If you don't however, you'd just be moving the if check to be the last time (as you'd still want the bottom padding on the last item). – ianhanniballake Apr 20 '15 at 18:04

RecyclerView.LayoutParams, LayoutParams subclass for children of RecyclerView . to create their own subclass of this LayoutParams class to store any additional required per-child view metadata about the layout. final void, wait(long arg0, int arg1). The :last-child selector matches every element that is the last child of its parent. Tip: p:last-child is equal to p:nth-last-child (1). Browser Support. The numbers in the table specifies the first browser version that fully supports the selector. css declarations; Previous CSS Selectors Reference Next

Comments
  • Use like this: <android.support.v7.widget.RecyclerView android:id="@+id/list" android:paddingBottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"/>
  • You could change the padding and margin in onBindViewHolder and then call setIsRecyclable(false) on the view holder
  • Yea that's how I usually do it, but I my RecyclerView is consisting of headers and each header has its own childs, that's why I need to set it programmatically to the last and first row of each header only
  • Not sure I understand. How is this related to headers inside the RecyclerView? Do you want the headers to have no padding? Even if that is the case, you can get rid of left & right padding using the same solution.
  • I have headers and each header has non-specific count of children so I want to add top padding/margin and the first child and bottom padding/margin at the last child of each header.
  • The only idea I had was set top and bottom margin into my header's layout but I was wondering if there's a smarter way to acheive it
  • In that case, you can simply add a fake item into the RecyclerView after your last child. That is the simplest, if not the smartest way to achieve this. :)
  • That's the easy solution, but I have headers and childs in my RecyclerView so I can not use it
  • @RedEagle Try and see if you get the desired effect but I'm sure it works.
  • This solution is better than the Item Decoration one. When I am using ListAdapter and submitting a different list, if the last item of the previous list did not change its position but new items were added after it, the former will keep its bottom padding even though it's not the last item in the list any more.
  • Hey it doesn't seem like RecyclerView.ItemDecoration get the resource class any more, I just added it as an argument in the constructor.
  • Goog solution! With reversed layout support: gist.github.com/Miha-x64/4e8754e72a1e65e3ca742744ea501f16
  • this answer must be upvoted! Though Subin Sebastian's answer is working good, however, with ItemDecoration spaces are not equal.
  • Awesome! That's perfect