RecyclerView在网格布局或者瀑布流布局下,如果要设置间距,可以使用ItemDecoration。
下面的代码是设置显示两列数据RecyclerView的情况。
cat.png
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//设置上下间距
if (isFirstRaw(parent.getChildAdapterPosition(view))) {
outRect.top = space;
} else {
outRect.bottom = space;
}
//判断左右列,方法1
//设置列间距
if (parent.getChildAdapterPosition(view) % 2 == 0) {
//左列
outRect.right = space / 2;
} else {
//右列
outRect.left = space / 2;
}