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
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;
        }
    }
}