首先需要用到自定义一个间距的speace
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.right = space;
// Add top margin only for the first item to avoid double space between items
if(parent.getChildPosition(view) ==0)
outRect.left = space;
}
}
然后自定义好之后我们来设置到我们的recyclerView上。
```
int space = UIUtils.dip2Px(15);
recyclerview.addItemDecoration(new SpacesItemDecoration(space));
此处附加UIUtils.dip2Px方法
public static int dip2Px(int dip){
float density = getResource().getDisplayMetrics().density;
int px = (int) ((float) dip * density +0.5F);
return px;
}
到此我们就实现动态设置间隔了
网友评论