-
Notifications
You must be signed in to change notification settings - Fork 2
/
PinnedStickerDecoration.kt
106 lines (100 loc) · 3.86 KB
/
PinnedStickerDecoration.kt
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
class PinnedStickerDecoration(context: Context,private val tests: MutableList<String>) : RecyclerView.ItemDecoration() {
private val textPaint = TextPaint()
private val bgdPaint = Paint()
private val dp1 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,1f,context.resources.displayMetrics)
init {
textPaint.apply {
isAntiAlias = true
color = ContextCompat.getColor(context, android.R.color.darker_gray)
typeface = Typeface.DEFAULT_BOLD
textSize = dp1 * 12
}
bgdPaint.color = ContextCompat.getColor(context, R.color.divide_color)
}
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
super.getItemOffsets(outRect, view, parent, state)
val pos = parent.getChildAdapterPosition(view)
if (pos == 0 || tests[pos][0].toUpperCase() != tests[pos - 1][0].toUpperCase()) {
outRect.top = (dp1 * 24).toInt()
} else {
outRect.top = dp1.toInt()
}
}
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDrawOver(c, parent, state)
for (i: Int in 0 until parent.childCount) {
val view = parent[i]
val pos = parent.getChildAdapterPosition(view)
if (view.top > dp1 * 24) {
// 第一个
if (pos == 0 || tests[pos][0].toUpperCase() != tests[pos - 1][0].toUpperCase()) {
// 顶部显示
c.drawRect(
view.left.toFloat(),
view.top - dp1 * 24,
view.right.toFloat(),
view.top.toFloat(),
bgdPaint
)
c.drawText(
tests[pos][0].toString().toUpperCase(Locale.ENGLISH),
view.left.toFloat() + dp1 * 16,
view.top - dp1 * 6,
textPaint
)
} else {
// 分隔线
c.drawRect(
view.left.toFloat(),
view.top - dp1,
view.right.toFloat(),
view.top.toFloat(),
bgdPaint
)
}
} else {
// 最后一个
if (pos + 1 < tests.size && tests[pos][0].toString().toUpperCase(Locale.ENGLISH) != tests[pos + 1][0].toString().toUpperCase(
Locale.ENGLISH
)
) {
// 拖动
val stickY: Float = min(view.bottom.toFloat(), dp1 * 24)
c.drawRect(
view.left.toFloat(),
stickY - dp1 * 24,
view.right.toFloat(),
stickY,
bgdPaint
)
c.drawText(
tests[pos][0].toString().toUpperCase(Locale.ENGLISH),
view.left.toFloat() + dp1 * 16,
stickY - dp1 * 6,
textPaint
)
} else {
// 置顶
c.drawRect(
view.left.toFloat(),
0f,
view.right.toFloat(),
dp1 * 24,
bgdPaint
)
c.drawText(
tests[pos][0].toString().toUpperCase(Locale.ENGLISH),
view.left.toFloat() + dp1 * 16,
dp1 * 18,
textPaint
)
}
}
}
}
}