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 SpacesItemDecoration extends RecyclerView.ItemDecoration { 
 | 
    private int space; 
 | 
    private int interval=-1;//间隔 
 | 
    private int topSpace; 
 | 
    private int type=-1;//0:左右,1:上下 
 | 
  
 | 
    //底部间距 
 | 
    public SpacesItemDecoration(int space) { 
 | 
        this.space = space; 
 | 
    } 
 | 
  
 | 
    //左右间距 
 | 
    public SpacesItemDecoration(int space, int interval) { 
 | 
        this.space = space; 
 | 
        this.interval=interval; 
 | 
    } 
 | 
  
 | 
    //顶部间距 
 | 
    public SpacesItemDecoration(int space, boolean isTop) { 
 | 
        if(isTop){ 
 | 
            this.topSpace=space; 
 | 
        }else { 
 | 
            this.space = space; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public SpacesItemDecoration(int space, int interval, boolean isTop) { 
 | 
        if(isTop){ 
 | 
            this.topSpace=space; 
 | 
        }else { 
 | 
            this.space = space; 
 | 
        } 
 | 
        this.interval=interval; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public void getItemOffsets(Rect outRect, View view, 
 | 
                               RecyclerView parent, RecyclerView.State state) { 
 | 
        /*outRect.left = space; 
 | 
        outRect.right = space; 
 | 
        outRect.bottom=space;*/ 
 | 
        outRect.bottom=space; 
 | 
        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;*/ 
 | 
        if(interval!=-1){ 
 | 
            if (parent.getChildPosition(view)%2 == 0){ 
 | 
                outRect.right = interval/2; 
 | 
            }else { 
 | 
                outRect.left = interval/2; 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
} 
 |