public class RecyclerViewDecoration extends RecyclerView.ItemDecoration {
private int horizontalSpace; // 整个RecyclerView与左右两侧的间距
private int verticalSpace; // 整个RecyclerView与上下的间距
private int leftMargin; // 每个item与左边的间距
private int topMargin; // 每个item与顶部的间距
private int rightMargin; // 每个item与右边的间距
private int bottomMargin; // 每个item与底部的间距
public RecyclerViewDecoration(int horizontalSpace, int verticalSpace) {
this(horizontalSpace, verticalSpace, 0, 0, 0, 0);
}
public RecyclerViewDecoration(int horizontalSpace, int verticalSpace, int margin) {
this(horizontalSpace, verticalSpace, margin, margin, margin, margin);
}
public RecyclerViewDecoration(int horizontalSpace, int verticalSpace, int leftMargin, int topMargin, int rightMargin, int bottomMargin) {
this.horizontalSpace = horizontalSpace;
this.verticalSpace = verticalSpace;
this.leftMargin = leftMargin;
this.topMargin = topMargin;
this.rightMargin = rightMargin;
this.bottomMargin = bottomMargin;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
// 得到当前Item在RecyclerView中的位置,从0开始
int position = parent.getChildAdapterPosition(view);
// 得到RecyclerView中Item的总个数
int co
recycleview添加边距
最新推荐文章于 2021-05-26 09:52:56 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)