回收视图中最后一个子元素的边距/填充

我试图在最后一行添加填充/边距底部,在第一行添加填充/边距顶部。我不能在项 xml 中执行此操作,因为它会影响到我的所有子项。

我的回收视图适配器中有标题和子标题,所以我不能使用

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

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

83291 次浏览

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

不需要将填充添加到顶部和底部项,只需将填充添加到回收视图的顶部和底部,并将 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" />

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

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

见填充将添加在所有方面,包括顶部和底部4 dp。然后,clipToPadd 参数确保子项没有被截断。现在,添加 4dp填充到您的子项目的所有方面,您就可以开始了。总的来说,你得到了8分之间的边缘和项目填充。

由于某些原因,旧的 clipToPadding=false解决方案对我不起作用

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

我使用这在科特林给底部边距最后一个项目 只有

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

我已经修改了惊人的答案@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;
}*/
}
}

在您的回收视图中添加 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"/>

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

长话短说:

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

设置此列表视图是否将其子级剪切到其填充,并在存在填充的情况下调整(但不剪切)任何 EdgeEffect 到填充区域。(1)