admin
2021-07-20 27bd1f81221b8c8e8047118a64c2beb7bc214bbb
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.yeshi.base.utils.ui;
 
/**
 * Created by weikou2015 on 2017/6/28.
 */
 
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
 
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
 
/**
 * This class is from the v7 samples of the Android SDK. It's not by me!
 * <p/>
 * See the license above for details.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    /**
     * 水平方向
     */
    public static final int HORIZONTAL = LinearLayoutManager.HORIZONTAL;
 
    /**
     * 垂直方向
     */
    public static final int VERTICAL = LinearLayoutManager.VERTICAL;
 
    // 画笔
    private Paint paint;
    // 布局方向
    private int orientation = 2;
    // 分割线颜色
    private int color;
    //列数  暂定最多2列
    private int numColumn = 1;
    // 分割线尺寸
    private int size = 2;
    // 分割线尺寸
    private boolean haveHeader;
    //是否有分割线
    private boolean haveDivider = true;
    private boolean isSet = false;
 
    public DividerItemDecoration() {
        this(VERTICAL);
        orientation = 2;
    }
 
    public DividerItemDecoration(int orientation) {
        this.orientation = orientation;
 
        paint = new Paint();
    }
 
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        outRect.bottom = size * 2;
        if (numColumn > 1) {
            if (haveHeader) {
                if (position == 0) {
                    return;
                } else {
                    position -= 1;
                }
            }
            if (position % numColumn == 0) {
                outRect.right = size;
            } else if (position % numColumn == (numColumn - 1)) {
                outRect.left = size;
            } else {
                outRect.left = size;
                outRect.right = size;
            }
        }
    }
 
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
//        if (orientation == VERTICAL) {
//            drawHorizontal(c, parent);
//        } else {
//            drawVertical(c, parent);
//        }
    }
 
    /**
     * 设置分割线颜色
     *
     * @param color 颜色
     */
    public void setColor(int color) {
        this.color = color;
        paint.setColor(color);
    }
 
    /**
     * 设置列数
     *
     * @param numColumn 列数
     */
    public void setNumColumn(int numColumn) {
        this.numColumn = numColumn;
    }
 
 
    /**
     * 是否加入header
     *
     * @param haveHeader header
     */
    public void isHaveHeader(boolean haveHeader) {
        this.haveHeader = haveHeader;
    }
 
    /**
     * 是否加入header
     *
     * @param haveDivider header
     */
    public void isHaveDivider(boolean haveDivider) {
        this.haveDivider = haveDivider;
    }
 
    /**
     * 设置分割线尺寸
     *
     * @param size 尺寸
     */
    public void setSize(int size) {
        this.size = size;
    }
 
    // 绘制垂直分割线
    protected void drawVertical(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
 
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (haveHeader && i == 1 && !isSet) {
                isSet = true;
                continue;
            }
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + size;
 
            c.drawRect(left, top, right, bottom, paint);
        }
    }
 
    // 绘制水平分割线
    protected void drawHorizontal(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();
 
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (haveHeader && i == 0 && !isSet) {
                isSet = true;
                continue;
            }
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + size;
 
            c.drawRect(left, top, right, bottom, paint);
        }
    }
}