@@ -2,7 +2,6 @@ package com.yhw.taglayout
22
33import android.annotation.SuppressLint
44import android.content.Context
5- import android.graphics.Rect
65import android.util.AttributeSet
76import android.util.DisplayMetrics
87import android.util.Log
@@ -18,7 +17,8 @@ private const val TAG = "TagLayout===>"
1817 * @author yhw
1918 */
2019class TagLayout : ViewGroup {
21- private val mViewRectMap = mutableMapOf<View , Rect >()
20+ private val mViewRectMap = mutableMapOf<View , MyRect >()
21+ private val mLineWidthMap = mutableMapOf<Int , Int >() // 存放行数和距离右侧的宽度
2222 private var choiceMode = ChoiceMode .None .choiceMode // 选择模式
2323 private var defChoicePosition: Int = 0 // 单选时默认选中
2424 private lateinit var mAdapter: TagAdapter
@@ -28,6 +28,8 @@ class TagLayout : ViewGroup {
2828 var onMultipleCheckedChangeListener: OnMultipleCheckedChangeListener ? = null
2929 private var mScreenWidth = 0 // 屏幕宽度
3030 private var mSingleChoiceSupportCancel = false // 单选是否支持取消
31+ private var mMeasuredWidth = 0 // 控件宽度
32+ private var mGravity = 0 // 对齐方式
3133
3234 constructor (context: Context ) : this (context, null )
3335 constructor (context: Context , attrs: AttributeSet ? ) : this (context, attrs, 0 )
@@ -41,6 +43,7 @@ class TagLayout : ViewGroup {
4143 defChoicePosition = ta.getInt(R .styleable.TagLayout_defaultChoicePosition , 0 )
4244 mSingleChoiceSupportCancel =
4345 ta.getBoolean(R .styleable.TagLayout_singleChoiceSupportCancel , false )
46+ mGravity = ta.getInt(R .styleable.TagLayout_gravity , GravityMode .Left .gravity)
4447 ta.recycle()
4548
4649 val windowManager = context
@@ -63,6 +66,7 @@ class TagLayout : ViewGroup {
6366 var lineWidth = 0 // 行宽
6467 var lineHeight = 0 // 行高
6568 Log .i(TAG , " widthSize is $widthSize widthMode is $widthMode mScreenWidth:${mScreenWidth} " )
69+ var lineNum = 0 // 行数
6670 measureChildren(widthMeasureSpec, heightMeasureSpec)
6771 for (i in 0 until childCount) {
6872 val childView = getChildAt(i)
@@ -77,6 +81,7 @@ class TagLayout : ViewGroup {
7781
7882 // 判断是否需要换行
7983 if (lineWidth + childWidth > widthSize - paddingLeft - paddingRight) {
84+ lineNum ++
8085 // 记录所有行中宽度最大的行
8186 width = max(width, lineWidth)
8287 // 换行后重置行宽为第一个view的宽度
@@ -91,7 +96,9 @@ class TagLayout : ViewGroup {
9196 // 记录每行view中高度最高的view为当前行高
9297 lineHeight = max(childHeight, lineHeight)
9398 }
94-
99+ Log .i(TAG , " lineCount============ $lineNum " )
100+ val srcLineWidth = if (mLineWidthMap[lineNum] != null ) mLineWidthMap[lineNum]!! else 0
101+ mLineWidthMap[lineNum] = max(lineWidth, srcLineWidth)
95102 if (choiceMode == ChoiceMode .SingleChoice .choiceMode) {
96103 if (i in 0 until childCount && i == defChoicePosition)
97104 childView.isSelected = true
@@ -105,7 +112,8 @@ class TagLayout : ViewGroup {
105112 val childTop = height + marginLayoutParams.topMargin + paddingTop
106113 val childBottom =
107114 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
109117 Log .i(
110118 TAG , " onMeasure left:${rect.left} top:${rect.top} ," +
111119 " right:${rect.right} ,bottom:${rect.bottom} measuredWidth:${childView.measuredWidth} "
@@ -119,22 +127,34 @@ class TagLayout : ViewGroup {
119127 height + = lineHeight
120128 }
121129 }
122- val measuredWidth =
130+ mMeasuredWidth =
123131 if (widthMode == MeasureSpec .EXACTLY ) widthSize else width + paddingLeft + paddingRight
124132 val measuredHeight =
125133 if (heightMode == MeasureSpec .EXACTLY ) heightSize else height + paddingTop + paddingBottom
126134 Log .i(
127135 TAG ,
128- " measuredWidth :${measuredWidth } measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth} "
136+ " measuredWidth :${mMeasuredWidth } measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth} "
129137 )
130- setMeasuredDimension(max(measuredWidth, width), measuredHeight)
138+
139+ setMeasuredDimension(max(mMeasuredWidth, width), measuredHeight)
131140 }
132141
133142 override fun onLayout (changed : Boolean , l : Int , t : Int , r : Int , b : Int ) {
134143 Log .i(TAG , " =========onLayout========== $changed $l $t $r $b " )
135144 var i = 0
136145 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)
138158 setItemListener(childView, i)
139159 i++
140160 }
@@ -322,6 +342,9 @@ class TagLayout : ViewGroup {
322342 fun onCheckedChanged (positionList : MutableList <Int >)
323343 }
324344
345+ /* *
346+ * 选择模式
347+ */
325348 enum class ChoiceMode (var choiceMode : Int ) {
326349 /* *
327350 * 非选择模式
@@ -338,4 +361,24 @@ class TagLayout : ViewGroup {
338361 */
339362 MultipleChoice (2 );
340363 }
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+ }
341384}
0 commit comments