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

时间:2015-11-17 10:20:54

标签: android

我试图在最后一行添加填充/边距底部,并在第一行添加填充/边距顶部。我不能在项目xml中这样做,因为它会影响我的所有Childs。

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

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

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

10 个答案:

答案 0 :(得分:162)

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

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

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

答案 1 :(得分:54)

您可以只将填充添加到RecyclerView的顶部和底部,并将'devices/secondDevice/messages/events/'属性设置为clipToPadding,而不是在顶部和底部项目中添加填充。

false

答案 2 :(得分:31)

使用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;
        }
    }
}

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

答案 3 :(得分:7)

 <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" />

添加android:clipToPadding="false"并  android:paddingBottom="100dp"在您的回收视图中。

答案 4 :(得分:2)

出于某种原因,旧的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);
        }
    }
}

答案 5 :(得分:1)

Java 相当于@Radesh 答案:

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (position == itemsList.size() - 1) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        params.bottomMargin = 100;
        holder.itemView.setLayoutParams(params);
    } else {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        params.bottomMargin = 0;
        holder.itemView.setLayoutParams(params);
    }
}

答案 6 :(得分:0)

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

答案 7 :(得分:0)

我已经修改了惊人的答案@snachmsm答案,以便更好,让您知道如何使用 正确

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;
        }*/
    }
}

答案 8 :(得分:0)

在您的recyclerview中添加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"/>

答案 9 :(得分:0)

长话短说:

    int freeSpaceAtBottom = 100; // the bottom free space in pixels
    myRecyclerView.setClipToPadding(false);
    myRecyclerView.setPadding(0,0,0,freeSpaceAtBottom);

setClipToPadding 设置此列表视图是否将其子视图裁剪到其填充,并将任何 EdgeEffect 调整大小(但不裁剪)到填充区域(如果存在填充)。 (1)