Margin/padding in last Child in RecyclerView

前端 未结 9 996
醉话见心
醉话见心 2021-01-30 05:56

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

I

9条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 06:42

    For some reason the old clipToPadding=false solution isn't working for me. So I added an 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);
            }
        }
    }
    

提交回复
热议问题