@@ -27,7 +27,8 @@ class TagLayout : ViewGroup {
2727 var onItemLongClickListener: OnItemLongClickListener ? = null
2828 var onSingleCheckedChangeListener: OnSingleCheckedChangeListener ? = null
2929 var onMultipleCheckedChangeListener: OnMultipleCheckedChangeListener ? = null
30- var mScreenWidth = 0 // 屏幕宽度
30+ private var mScreenWidth = 0 // 屏幕宽度
31+ private var mSingleChoiceSupportCancel = false // 单选是否支持取消
3132
3233 constructor (context: Context ) : this (context, null )
3334 constructor (context: Context , attrs: AttributeSet ? ) : this (context, attrs, 0 )
@@ -39,6 +40,7 @@ class TagLayout : ViewGroup {
3940 val ta = context.obtainStyledAttributes(attrs, R .styleable.TagLayout )
4041 choiceMode = ta.getInt(R .styleable.TagLayout_choiceMode , ChoiceMode .None .choiceMode)
4142 defChoicePosition = ta.getInt(R .styleable.TagLayout_defaultChoicePosition , 0 )
43+ mSingleChoiceSupportCancel = ta.getBoolean(R .styleable.TagLayout_singleChoiceSupportCancel , false )
4244 ta.recycle()
4345
4446 val windowManager = context
@@ -139,7 +141,11 @@ class TagLayout : ViewGroup {
139141 onItemClickListener?.onItemClick(position, it)
140142 }
141143 if (choiceMode == ChoiceMode .SingleChoice .choiceMode) {
142- this .defChoicePosition = position
144+ if (it.isSelected) {
145+ this .defChoicePosition = position
146+ } else {
147+ this .defChoicePosition = - 1
148+ }
143149 if (onSingleCheckedChangeListener != null ) {
144150 onSingleCheckedChangeListener?.onCheckedChanged(defChoicePosition)
145151 }
@@ -154,10 +160,10 @@ class TagLayout : ViewGroup {
154160 if (onItemLongClickListener != null ) {
155161 childView.isClickable = true
156162 childView.isFocusable = true
157- childView.setOnLongClickListener( OnLongClickListener { v ->
163+ childView.setOnLongClickListener { v ->
158164 onItemLongClickListener?.onItemLongClick(i, v)
159165 true
160- })
166+ }
161167 }
162168 i++
163169 }
@@ -175,7 +181,15 @@ class TagLayout : ViewGroup {
175181 continue
176182 }
177183 if (choiceMode == ChoiceMode .SingleChoice .choiceMode) {
178- view.isSelected = index == position
184+ if (mSingleChoiceSupportCancel) {
185+ if (index == position) {
186+ view.isSelected = ! view.isSelected
187+ } else {
188+ view.isSelected = false
189+ }
190+ } else {
191+ view.isSelected = index == position
192+ }
179193 } else if (choiceMode == ChoiceMode .MultipleChoice .choiceMode) {
180194 if (index == position) {
181195 view.isSelected = ! view.isSelected
0 commit comments