RecyclerDemacration.java
5.39 KB
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
package com.cnlive.goldenline.ui.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
/**
* RecyclerView 间隔线 可以传递颜色值
* Created on 2017/2/23
* Version V1.0
*/
public class RecyclerDemacration extends RecyclerView.ItemDecoration {
//布局方向 既有垂直 又有 水平 即 用于 GridLaytoutManager
protected static final int ORIENTATION_HYBIRD = 0x2;
/**
* RecyclerView布局方向 默认垂直
*/
private int mOrientation = LinearLayoutManager.VERTICAL;
/**
* 画笔
*/
private Paint mPaint;
/**
* 间隔线高度
*/
private int mDividerHeight = 2;
private Drawable mDrawable;
/**
* @param mOrientation
* @param color Color.BLACK 或者 getResource().getColor(R.color.xxx);
*/
public RecyclerDemacration(@NonNull Context context, int mOrientation, int color, int mDividerHeight) {
if (mOrientation != LinearLayoutManager.VERTICAL && mOrientation != LinearLayoutManager.HORIZONTAL && mOrientation != ORIENTATION_HYBIRD)
throw new IllegalArgumentException("orientation is Illegal , Must be LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL or ORIENTATION_HYBIRD");
this.mOrientation = mOrientation;
mPaint = new Paint();
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(color);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
this.mDividerHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mDividerHeight, context.getResources().getDisplayMetrics());
}
public RecyclerDemacration(Context context, int mOrientation, Drawable mDrawable) {
if (mOrientation != LinearLayoutManager.VERTICAL && mOrientation != LinearLayoutManager.HORIZONTAL && mOrientation != ORIENTATION_HYBIRD)
throw new IllegalArgumentException("orientation is Illegal , Must be LinearLayoutManager.VERTICAL or LinearLayoutManager.HORIZONTAL or ORIENTATION_HYBIRD");
this.mOrientation = mOrientation;
this.mDrawable = mDrawable;
}
// 子Item绘制之前
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
}
// 子item 绘制之后
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
switch (mOrientation) {
case LinearLayoutManager.VERTICAL:
drawVertical(c, parent);
break;
case LinearLayoutManager.HORIZONTAL:
drawHorizontal(c, parent);
break;
case ORIENTATION_HYBIRD:
drawVertical(c, parent);
drawHorizontal(c, parent);
break;
default:
drawVertical(c, parent);
break;
}
}
@Override
public void getItemOffsets(Rect outRect, @NonNull View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
}
private void drawVertical(@NonNull Canvas c, @NonNull RecyclerView parent) {
int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
int left = child.getLeft() - layoutParams.leftMargin;
int right = child.getRight() + layoutParams.rightMargin;
if (mDrawable != null) { // 优先绘制Drawable
right += mDrawable.getIntrinsicWidth();
final int bottom = top + mDrawable.getIntrinsicHeight();
mDrawable.setBounds(left, top, right, bottom);
mDrawable.draw(c);
} else {//其次绘制纯色
right += mDividerHeight;
final int bottom = top + mDividerHeight;
c.drawRect(left, top, right, bottom, mPaint);
}
}
}
private void drawHorizontal(@NonNull Canvas c, @NonNull RecyclerView parent) {
int childSize = parent.getChildCount();
for (int i = 0; i < childSize; i++) {
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getTop() - layoutParams.topMargin;
int bottom = child.getBottom() + layoutParams.bottomMargin;
int left = child.getRight() + layoutParams.rightMargin;
if (mDrawable != null) { // 优先绘制Drawable
final int right = left + mDrawable.getIntrinsicWidth();
mDrawable.setBounds(left, top, right, bottom);
mDrawable.draw(c);
} else {//其次绘制纯色
final int right = left + mDividerHeight;
c.drawRect(left, top, right, bottom, mPaint);
}
}
}
}