AppinfoiItemDecoration decor = new AppinfoiItemDecoration(); //設置條目的間距 recycler.addItemDecoration(decor);
複寫 getItemOffsets 方法設置left right bottom 和top 的距離
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { //不是第一個的格子都設一個左邊和底部的間距 int pos = parent.getChildAdapterPosition(view); outRect.left = 10; if (pos != 0) { if (pos % 2 == 0) { //下面一行 outRect.bottom =30; outRect.top = 5; } else { //上面一行 outRect.top = 30; outRect.bottom = 5; } } }