释放双眼,带上耳机,听听看~!
RecycleView 虽然相对ListView GridView 做了很多的优化。方便开发者的使用,性能也得到了很大的提高,但是悲剧的是RecycleView 设置Item间距并没有那么容易。。。
java代码设置
调用
//RecycleView 增加边距
int spacingInPixels = 8;
mClassifyItemrecy.addItemDecoration(new SpacesItemDecoration(spacingInPixels));
mClassifyItemrecy.setAdapter(mAdapter);
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;
}
}
如果你了别人的RecycleView 上拉加载下拉刷新,addItemDecoration 这个方法 不一定会给你加上,你可以在源码上自行添加。
public void addItemDecoration(RecyclerView.ItemDecoration decor) {
mRecyclerView.addItemDecoration(decor,-1);
}