今天有人问到这方面问题,趁着礼拜天 分享给大家 ,直接上代码
首先先将下面的复制的你的代码中
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));}@Overridepublic 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);
效果图demo地址。。。。