@@ -10,7 +10,10 @@ import com.skyd.anivu.ui.adapter.variety.VarietyAdapter
10
10
import kotlin.math.roundToInt
11
11
12
12
13
- class AniVuItemDecoration : RecyclerView .ItemDecoration () {
13
+ class AniVuItemDecoration (
14
+ private val hItemSpace : Int = H_ITEM_SPACE ,
15
+ private val horizontalSpace : Int = HORIZONTAL_PADDING ,
16
+ ) : RecyclerView.ItemDecoration() {
14
17
override fun getItemOffsets (
15
18
outRect : Rect ,
16
19
view : View ,
@@ -27,67 +30,63 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
27
30
// 注意这里使用getChildLayoutPosition的目的
28
31
// 如果使用getChildAdapterPosition,刷新的时候可能会(边框)闪动一下,(返回-1)
29
32
?.getOrNull(parent.getChildLayoutPosition(view))
30
- if (needVerticalMargin(item?.javaClass)) {
31
- outRect.top = 10 .dp
32
- outRect.bottom = 2 .dp
33
- }
34
33
if (spanSize == MAX_SPAN_SIZE ) {
35
34
/* *
36
35
* 只有一列
37
36
*/
38
37
if (noHorizontalMargin(item?.javaClass)) return
39
- outRect.left = HORIZONTAL_PADDING
40
- outRect.right = HORIZONTAL_PADDING
38
+ outRect.left = horizontalSpace
39
+ outRect.right = horizontalSpace
41
40
} else if (spanSize == MAX_SPAN_SIZE / 2 ) {
42
41
/* *
43
42
* 只有两列,没有在中间的item
44
- * 2x = ITEM_SPACING
43
+ * 2x = hItemSpace
45
44
*/
46
- val x: Int = (ITEM_SPACING / 2f ).roundToInt()
45
+ val x: Int = (hItemSpace / 2f ).roundToInt()
47
46
if (spanIndex == 0 ) {
48
- outRect.left = HORIZONTAL_PADDING
47
+ outRect.left = horizontalSpace
49
48
outRect.right = x
50
49
} else {
51
50
outRect.left = x
52
- outRect.right = HORIZONTAL_PADDING
51
+ outRect.right = horizontalSpace
53
52
}
54
53
} else if (spanSize == MAX_SPAN_SIZE / 3 ) {
55
54
/* *
56
55
* 只有三列,一个在中间的item
57
- * HORIZONTAL_PADDING + x = 2y
58
- * x + y = ITEM_SPACING
56
+ * horizontalSpace + x = 2y
57
+ * x + y = hItemSpace
59
58
*/
60
- val y: Int = ((HORIZONTAL_PADDING + ITEM_SPACING ) / 3f ).roundToInt()
61
- val x: Int = ITEM_SPACING - y
59
+ val y: Int = ((horizontalSpace + hItemSpace ) / 3f ).roundToInt()
60
+ val x: Int = hItemSpace - y
62
61
if (spanIndex == 0 ) {
63
- outRect.left = HORIZONTAL_PADDING
62
+ outRect.left = horizontalSpace
64
63
outRect.right = x
65
64
} else if (spanIndex + spanSize == MAX_SPAN_SIZE ) {
66
65
// 最右侧最后一个
67
66
outRect.left = x
68
- outRect.right = HORIZONTAL_PADDING
67
+ outRect.right = horizontalSpace
69
68
} else {
70
69
outRect.left = y
71
70
outRect.right = y
72
71
}
73
72
} else if (spanSize == MAX_SPAN_SIZE / 5 ) {
74
73
/* *
75
74
* 只有五列
76
- * HORIZONTAL_PADDING + x = y + z
77
- * x + y = ITEM_SPACING
78
- * z + (HORIZONTAL_PADDING + x) / 2 = ITEM_SPACING
75
+ * horizontalSpace + x = y + z
76
+ * x + y = hItemSpace
77
+ * z + (horizontalSpace + x) / 2 = hItemSpace
79
78
*/
80
- val x: Int = ((4 * ITEM_SPACING - 3 * HORIZONTAL_PADDING ) / 5f ).roundToInt()
81
- val y: Int = ITEM_SPACING - x
82
- val z: Int = HORIZONTAL_PADDING + x - y
79
+ val x: Int = ((4 * hItemSpace - 3 * horizontalSpace ) / 5f ).roundToInt()
80
+ val y: Int = hItemSpace - x
81
+ val z: Int = horizontalSpace + x - y
83
82
if (spanIndex == 0 ) {
84
83
// 最左侧第一个
85
- outRect.left = HORIZONTAL_PADDING
84
+ outRect.left = horizontalSpace
86
85
outRect.right = x
87
86
} else if (spanIndex + spanSize == MAX_SPAN_SIZE ) {
88
87
// 最右侧最后一个
89
88
outRect.left = x
90
- outRect.right = HORIZONTAL_PADDING
89
+ outRect.right = horizontalSpace
91
90
} else if (spanIndex == spanSize) {
92
91
// 第二个
93
92
outRect.left = y
@@ -98,8 +97,8 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
98
97
outRect.right = y
99
98
} else {
100
99
// 最中间的
101
- outRect.left = ((HORIZONTAL_PADDING + x) / 2f ).roundToInt()
102
- outRect.right = ((HORIZONTAL_PADDING + x) / 2f ).roundToInt()
100
+ outRect.left = ((horizontalSpace + x) / 2f ).roundToInt()
101
+ outRect.right = ((horizontalSpace + x) / 2f ).roundToInt()
103
102
}
104
103
} else {
105
104
/* *
@@ -108,28 +107,28 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
108
107
if ((MAX_SPAN_SIZE / spanSize) % 2 == 0 ) {
109
108
/* *
110
109
* 偶数个item
111
- * HORIZONTAL_PADDING + x = y + ITEM_SPACING / 2
112
- * x + y = ITEM_SPACING
110
+ * horizontalSpace + x = y + hItemSpace / 2
111
+ * x + y = hItemSpace
113
112
*/
114
- val y: Int = ((HORIZONTAL_PADDING + ITEM_SPACING / 2f ) / 2f ).roundToInt()
115
- val x: Int = ITEM_SPACING - y
113
+ val y: Int = ((horizontalSpace + hItemSpace / 2f ) / 2f ).roundToInt()
114
+ val x: Int = hItemSpace - y
116
115
if (spanIndex == 0 ) {
117
116
// 最左侧第一个
118
- outRect.left = HORIZONTAL_PADDING
117
+ outRect.left = horizontalSpace
119
118
outRect.right = x
120
119
} else if (spanIndex + spanSize == MAX_SPAN_SIZE ) {
121
120
// 最右侧最后一个
122
121
outRect.left = x
123
- outRect.right = HORIZONTAL_PADDING
122
+ outRect.right = horizontalSpace
124
123
} else {
125
124
// 中间的项目
126
125
if (spanIndex < MAX_SPAN_SIZE / 2 ) {
127
126
// 左侧部分
128
127
outRect.left = y
129
- outRect.right = ITEM_SPACING / 2
128
+ outRect.right = hItemSpace / 2
130
129
} else {
131
130
// 右侧部分
132
- outRect.left = ITEM_SPACING / 2
131
+ outRect.left = hItemSpace / 2
133
132
outRect.right = y
134
133
}
135
134
}
@@ -142,8 +141,9 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
142
141
}
143
142
144
143
companion object {
145
- val ITEM_SPACING : Int = 12 .dp
144
+ val H_ITEM_SPACE : Int = 12 .dp
146
145
val HORIZONTAL_PADDING : Int = 16 .dp
146
+ val VERTICAL_PADDING : Int = 16 .dp
147
147
148
148
private val noHorizontalMarginType: Set <Class <* >> = setOf (
149
149
@@ -153,14 +153,5 @@ class AniVuItemDecoration : RecyclerView.ItemDecoration() {
153
153
clz ? : return true
154
154
return clz in noHorizontalMarginType
155
155
}
156
-
157
- private val needVerticalMarginType: Set <Class <* >> = setOf (
158
-
159
- )
160
-
161
- fun needVerticalMargin (clz : Class <* >? ): Boolean {
162
- clz ? : return false
163
- return clz in needVerticalMarginType
164
- }
165
156
}
166
157
}
0 commit comments