rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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;
            }
        }
    }
}