public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpaceItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//不是第一个的格子都设一个左边和底部的间距
outRect.left = space;
outRect.bottom = space;
//由于每行都只有3个,所以第一个都是3的倍数,把左边距设为0
//这里 % 2 是不固定,根据你自定的列数来决定,你显示3列,就填3,显示5列就填5
if (parent.getChildLayoutPosition(view) % 2 == 0) {
outRect.left = 0;
}
}
}
//使用,传的参数就是你想要的间距宽度,根据自己需求填写
recyclerView.addItemDecoration(new SpaceItemDecoration(5));