今天有人问到这方面问题,趁着礼拜天 分享给大家 ,直接上代码
首先先将下面的复制的你的代码中
public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {
private int mItemOffset;
public ItemOffsetDecoration(int itemOffset) {
mItemOffset = itemOffset;
}
public ItemOffsetDecoration(Context context, int itemOffsetId) {
this(context.getResources().getDimensionPixelSize(itemOffsetId));
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.set(mItemOffset, mItemOffset, mItemOffset, mItemOffset);
}
}
他的使用如下
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
ItemOffsetDecoration itemOffsetDecoration = new ItemOffsetDecoration(this, R.dimen.dp_5);
recyclerView.addItemDecoration(itemOffsetDecoration);
adapter = new GridAdapter(list);
标签:outRect,ItemOffsetDecoration,int,mItemOffset,九宫格,android,RecyclerView,public
来源: https://blog.csdn.net/qq_33210042/article/details/94161986