package com.doumee.lib_coremodel.view.recyclerview; /** * Created by MSI on 2018/5/31. */ import android.graphics.Rect; import android.view.View; import androidx.recyclerview.widget.RecyclerView; /** * 设置item间距 */ public class SpacesDecorationWithCount extends RecyclerView.ItemDecoration { private int space;//间隔 private int count;//数目 private int topSpace; private int bottomSpace; private int type=0; public SpacesDecorationWithCount(int space, int count) { this.space = space; this.count=count; } public SpacesDecorationWithCount(int space, int count, int type) { this.space = space; this.count=count; this.type=type; } public SpacesDecorationWithCount(int space, int count, int topSpace, int bottomSpace) { this.space = space; this.count = count; this.topSpace = topSpace; this.bottomSpace = bottomSpace; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.bottom=bottomSpace; outRect.top=topSpace; // Add top margin only for the first item to avoid double space between items /*if (parent.getChildPosition(view) == 0) outRect.top = space;*/ float f = 1.0f/(count-1); int space50 = (int) (space*f); int space150 = space - space50; int sp = (int) (space * 0.5f); int sp1 = space150 - space50; if(type==0){ int po=parent.getChildAdapterPosition(view)%count; if (po == 0){ outRect.right = space150; }else if(po==count-1){ outRect.left = space150; }else { outRect.left = space50; outRect.right = sp1; } }else { outRect.left=sp; outRect.right=sp; } } }