这是LinearLayoutManager设置Item间距的的一个辅助类
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
private int space;
public SpacesItemDecoration(int space) {
this.space = space;
}
@Override
public void getItemOffsets(Rect outRect, View view,
RecyclerView parent, RecyclerView.State state) {
outRect.left = space;
outRect.right = space;
outRect.bottom = space;
// Add top margin only for the first item to avoid double space between items
if (parent.getChildPosition(view) == 0)
outRect.top = space;
}
}
设置item间距
int space = 8;
mRecyclerView.addItemDecoration(new SpacesItemDecoration(space));
mRecyclerView.setAdapter(myAdapter);