如何在RecyclerView中的项目之间添加空格?

时间:2018-01-24 11:32:09

标签: android

我创建了一个Recyclerview,如下图所示 如何管理?

enter image description here

1 个答案:

答案 0 :(得分:0)

将此添加到singlerow.xml的根布局:

android:layout_marginBottom="10dp"

<强>更新

  

垂直间距ItemDecoration

扩展ItemDecoration,添加自定义构造函数,将空间高度作为参数并覆盖getItemOffsets()方法:

public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int verticalSpaceHeight;

    public VerticalSpaceItemDecoration(int verticalSpaceHeight) {
        this.verticalSpaceHeight = verticalSpaceHeight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        outRect.bottom = verticalSpaceHeight;
    }
}

如果您不想在最后一项下方插入空格,请添加以下条件:

if (parent.getChildAdapterPosition(view) != parent.getAdapter().getItemCount() - 1) {
            outRect.bottom = verticalSpaceHeight;
}

注意:您还可以修改outRect.top,outRect.left和outRect.right属性以获得所需的效果。