Linjiajia
2023-03-20 25b98735c5c0cbe40a07ae91a9fb2b204d9166b7
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.android.app_base.widget;
 
import android.graphics.Rect;
import android.view.View;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
/**
* @author Ljj
* @date 2023.03.18. 20:46
* @desc
*/public class LinearItemDecoration extends RecyclerView.ItemDecoration {
    private int leftSpace;
    private int rightSpace;
    private int topSpace;
    private int bottomSpace;
 
    private int firstTop = -1;
 
    public LinearItemDecoration(){
 
    }
 
    public LinearItemDecoration(int space) {
        leftSpace = rightSpace = topSpace = bottomSpace = space;
    }
 
    public LinearItemDecoration(int horizontalSpace,int verticalSpace) {
        rightSpace = leftSpace = horizontalSpace;
        topSpace = bottomSpace = verticalSpace;
    }
 
    public LinearItemDecoration(int leftSpace, int rightSpace, int topSpace, int bottomSpace) {
        this.leftSpace = leftSpace;
        this.rightSpace = rightSpace;
        this.topSpace = topSpace;
        this.bottomSpace = bottomSpace;
    }
 
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.left = leftSpace;
        outRect.right = rightSpace;
        outRect.bottom = bottomSpace;
        outRect.top = topSpace;
        if ( firstTop != -1 && firstTop >= 0){
            if (parent.getChildLayoutPosition(view) == 0)
                outRect.top = firstTop;
        }
    }
 
    public void setTopSpace(int topSpace) {
        this.topSpace = topSpace;
    }
 
    public void setLeftSpace(int leftSpace) {
        this.leftSpace = leftSpace;
    }
 
    public void setRightSpace(int rightSpace) {
        this.rightSpace = rightSpace;
    }
 
    public void setBottomSpace(int bottomSpace) {
        this.bottomSpace = bottomSpace;
    }
 
    public void setHorizontalSpace(int horizontalSpace){
        rightSpace = leftSpace = horizontalSpace;
    }
    public void setVerticalSpace(int verticalSpace){
        rightSpace = leftSpace = verticalSpace;
    }
 
    public void setFirstTop(int firstTop) {
        this.firstTop = firstTop;
    }
 
    public int getLeftSpace() {
        return leftSpace;
    }
 
    public int getRightSpace() {
        return rightSpace;
    }
 
    public int getTopSpace() {
        return topSpace;
    }
 
    public int getBottomSpace() {
        return bottomSpace;
    }
 
    public int getFirstTop() {
        return firstTop;
    }
}