RecyclerView中最后一个Child的边距/填充

Red*_*gle 97 android padding android-recyclerview

我正在尝试在最后一行添加Padding/Margin Bottom,在第一行添加Padding/Margin Top.我不能在项目xml中这样做,因为它会影响我的所有Childs.

我的RecyclerView适配器中有标题和子项,所以我无法使用

   android:padding="4dp"
   android:clipToPadding="false"
Run Code Online (Sandbox Code Playgroud)

我需要在每个标题的最后一行单独使用它

Sub*_*ian 193

这个问题更容易解决.您可以对RecylerView自身应用必要的填充并设置clipToPadding为false,否则填充将切断滚动区域.这是一个例子

<android.support.v7.widget.RecyclerView
    android:padding="4dp"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

看到填充将在所有方面添加4dp,包括顶部和底部.然后clipToPadding参数确保您的子项不会被切断.现在,4dp为孩子的物品添加填充物,你就可以了.总共可以在侧面和物品之间获得8dp填充.


Col*_*iza 70

您可以将填充添加到RecyclerView的顶部和底部,并将clipToPadding属性设置为,而不是向顶部和底部项添加填充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" />
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案比物品装饰解决方案更好。当我使用 ListAdapter 并提交不同的列表时,如果前一个列表的最后一项没有更改其位置,但在其后添加了新项目,则前者将保留其底部填充,即使它不是列表中的最后一项更多的。 (2认同)

sna*_*msm 34

用途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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

//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));
Run Code Online (Sandbox Code Playgroud)

  • Goog解决方案!使用反向布局支持:https://gist.github.com/Miha-x64/4e8754e72a1e65e3ca742744ea501f16 (2认同)
  • 这个答案必须得到赞成!虽然Subin Sebastian的答案运作良好,但是ItemDecoration空间并不相同. (2认同)

小智 21

在您的回收站视图中添加 android:clipToPadding="false" 和 android:paddingBottom="65dp"。如果您在回收器视图单元上使用 fab 菜单按钮和操作。

<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"/>
Run Code Online (Sandbox Code Playgroud)


dar*_*tel 17

 <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" />
Run Code Online (Sandbox Code Playgroud)

在您的recyclerview中添加android:clipToPadding="false"android:paddingBottom="100dp"

  • 惊人的!那很完美 (2认同)

Rad*_*esh 15

我用这个在科特林给下边距到最后一个项目

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 ...
}
Run Code Online (Sandbox Code Playgroud)


kas*_*sim 5

由于某种原因老clipToPadding=false解决方案对我不起作用。所以我添加了一个 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)