@@ -2,7 +2,6 @@ package com.yhw.taglayout
2
2
3
3
import android.annotation.SuppressLint
4
4
import android.content.Context
5
- import android.graphics.Rect
6
5
import android.util.AttributeSet
7
6
import android.util.DisplayMetrics
8
7
import android.util.Log
@@ -18,7 +17,8 @@ private const val TAG = "TagLayout===>"
18
17
* @author yhw
19
18
*/
20
19
class TagLayout : ViewGroup {
21
- private val mViewRectMap = mutableMapOf<View , Rect >()
20
+ private val mViewRectMap = mutableMapOf<View , MyRect >()
21
+ private val mLineWidthMap = mutableMapOf<Int , Int >() // 存放行数和距离右侧的宽度
22
22
private var choiceMode = ChoiceMode .None .choiceMode // 选择模式
23
23
private var defChoicePosition: Int = 0 // 单选时默认选中
24
24
private lateinit var mAdapter: TagAdapter
@@ -28,6 +28,8 @@ class TagLayout : ViewGroup {
28
28
var onMultipleCheckedChangeListener: OnMultipleCheckedChangeListener ? = null
29
29
private var mScreenWidth = 0 // 屏幕宽度
30
30
private var mSingleChoiceSupportCancel = false // 单选是否支持取消
31
+ private var mMeasuredWidth = 0 // 控件宽度
32
+ private var mGravity = 0 // 对齐方式
31
33
32
34
constructor (context: Context ) : this (context, null )
33
35
constructor (context: Context , attrs: AttributeSet ? ) : this (context, attrs, 0 )
@@ -41,6 +43,7 @@ class TagLayout : ViewGroup {
41
43
defChoicePosition = ta.getInt(R .styleable.TagLayout_defaultChoicePosition , 0 )
42
44
mSingleChoiceSupportCancel =
43
45
ta.getBoolean(R .styleable.TagLayout_singleChoiceSupportCancel , false )
46
+ mGravity = ta.getInt(R .styleable.TagLayout_gravity , GravityMode .Left .gravity)
44
47
ta.recycle()
45
48
46
49
val windowManager = context
@@ -63,6 +66,7 @@ class TagLayout : ViewGroup {
63
66
var lineWidth = 0 // 行宽
64
67
var lineHeight = 0 // 行高
65
68
Log .i(TAG , " widthSize is $widthSize widthMode is $widthMode mScreenWidth:${mScreenWidth} " )
69
+ var lineNum = 0 // 行数
66
70
measureChildren(widthMeasureSpec, heightMeasureSpec)
67
71
for (i in 0 until childCount) {
68
72
val childView = getChildAt(i)
@@ -77,6 +81,7 @@ class TagLayout : ViewGroup {
77
81
78
82
// 判断是否需要换行
79
83
if (lineWidth + childWidth > widthSize - paddingLeft - paddingRight) {
84
+ lineNum ++
80
85
// 记录所有行中宽度最大的行
81
86
width = max(width, lineWidth)
82
87
// 换行后重置行宽为第一个view的宽度
@@ -91,7 +96,9 @@ class TagLayout : ViewGroup {
91
96
// 记录每行view中高度最高的view为当前行高
92
97
lineHeight = max(childHeight, lineHeight)
93
98
}
94
-
99
+ Log .i(TAG , " lineCount============ $lineNum " )
100
+ val srcLineWidth = if (mLineWidthMap[lineNum] != null ) mLineWidthMap[lineNum]!! else 0
101
+ mLineWidthMap[lineNum] = max(lineWidth, srcLineWidth)
95
102
if (choiceMode == ChoiceMode .SingleChoice .choiceMode) {
96
103
if (i in 0 until childCount && i == defChoicePosition)
97
104
childView.isSelected = true
@@ -105,7 +112,8 @@ class TagLayout : ViewGroup {
105
112
val childTop = height + marginLayoutParams.topMargin + paddingTop
106
113
val childBottom =
107
114
height + childHeight - marginLayoutParams.bottomMargin + paddingTop
108
- val rect = Rect (childLeft, childTop, childRight, childBottom)
115
+ val rect = MyRect (childLeft, childTop, childRight, childBottom)
116
+ rect.lineNum = lineNum
109
117
Log .i(
110
118
TAG , " onMeasure left:${rect.left} top:${rect.top} ," +
111
119
" right:${rect.right} ,bottom:${rect.bottom} measuredWidth:${childView.measuredWidth} "
@@ -119,22 +127,34 @@ class TagLayout : ViewGroup {
119
127
height + = lineHeight
120
128
}
121
129
}
122
- val measuredWidth =
130
+ mMeasuredWidth =
123
131
if (widthMode == MeasureSpec .EXACTLY ) widthSize else width + paddingLeft + paddingRight
124
132
val measuredHeight =
125
133
if (heightMode == MeasureSpec .EXACTLY ) heightSize else height + paddingTop + paddingBottom
126
134
Log .i(
127
135
TAG ,
128
- " measuredWidth :${measuredWidth } measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth} "
136
+ " measuredWidth :${mMeasuredWidth } measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth} "
129
137
)
130
- setMeasuredDimension(max(measuredWidth, width), measuredHeight)
138
+
139
+ setMeasuredDimension(max(mMeasuredWidth, width), measuredHeight)
131
140
}
132
141
133
142
override fun onLayout (changed : Boolean , l : Int , t : Int , r : Int , b : Int ) {
134
143
Log .i(TAG , " =========onLayout========== $changed $l $t $r $b " )
135
144
var i = 0
136
145
for ((childView, rect) in mViewRectMap) {
137
- childView.layout(rect.left, rect.top, rect.right, rect.bottom)
146
+ val lineRightWidth = mLineWidthMap[rect.lineNum]
147
+ var moveGap = 0
148
+ if (mGravity == GravityMode .RIGHT .gravity) {
149
+ if (lineRightWidth != null && lineRightWidth > 0 ) {
150
+ moveGap = mMeasuredWidth - lineRightWidth
151
+ }
152
+ } else if (mGravity == GravityMode .CENTER .gravity) {
153
+ if (lineRightWidth != null && lineRightWidth > 0 ) {
154
+ moveGap = (mMeasuredWidth - lineRightWidth) / 2
155
+ }
156
+ }
157
+ childView.layout(rect.left + moveGap, rect.top, rect.right + moveGap, rect.bottom)
138
158
setItemListener(childView, i)
139
159
i++
140
160
}
@@ -322,6 +342,9 @@ class TagLayout : ViewGroup {
322
342
fun onCheckedChanged (positionList : MutableList <Int >)
323
343
}
324
344
345
+ /* *
346
+ * 选择模式
347
+ */
325
348
enum class ChoiceMode (var choiceMode : Int ) {
326
349
/* *
327
350
* 非选择模式
@@ -338,4 +361,24 @@ class TagLayout : ViewGroup {
338
361
*/
339
362
MultipleChoice (2 );
340
363
}
364
+
365
+ /* *
366
+ * 对齐方式
367
+ */
368
+ enum class GravityMode (var gravity : Int ) {
369
+ /* *
370
+ * 居左对齐
371
+ */
372
+ Left (0 ),
373
+
374
+ /* *
375
+ * 居中对齐
376
+ */
377
+ CENTER (1 ),
378
+
379
+ /* *
380
+ * 居右对齐
381
+ */
382
+ RIGHT (2 );
383
+ }
341
384
}
0 commit comments