Skip to content

Commit 9dd4aac

Browse files
committed
单选支持取消选中
1 parent 7ff2303 commit 9dd4aac

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

library/src/main/java/com/yhw/taglayout/TagAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package com.yhw.taglayout
33
import android.view.View
44
import android.view.ViewGroup
55

6-
abstract class TagAdapter{
6+
abstract class TagAdapter {
77

88
abstract fun onCreateView(parent: ViewGroup): View
99

10-
abstract fun onBindView(itemView:View, position: Int)
10+
abstract fun onBindView(itemView: View, position: Int)
1111

1212
abstract fun getItemCount(): Int
1313
}

library/src/main/java/com/yhw/taglayout/TagLayout.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class TagLayout : ViewGroup {
4040
val ta = context.obtainStyledAttributes(attrs, R.styleable.TagLayout)
4141
choiceMode = ta.getInt(R.styleable.TagLayout_choiceMode, ChoiceMode.None.choiceMode)
4242
defChoicePosition = ta.getInt(R.styleable.TagLayout_defaultChoicePosition, 0)
43-
mSingleChoiceSupportCancel = ta.getBoolean(R.styleable.TagLayout_singleChoiceSupportCancel, false)
43+
mSingleChoiceSupportCancel =
44+
ta.getBoolean(R.styleable.TagLayout_singleChoiceSupportCancel, false)
4445
ta.recycle()
4546

4647
val windowManager = context
@@ -122,7 +123,10 @@ class TagLayout : ViewGroup {
122123
if (widthMode == MeasureSpec.EXACTLY) widthSize else width + paddingLeft + paddingRight
123124
val measuredHeight =
124125
if (heightMode == MeasureSpec.EXACTLY) heightSize else height + paddingTop + paddingBottom
125-
Log.i(TAG, "measuredWidth :${measuredWidth} measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth}")
126+
Log.i(
127+
TAG,
128+
"measuredWidth :${measuredWidth} measuredHeight:${measuredHeight} width:${width} lineWidth:${lineWidth}"
129+
)
126130
setMeasuredDimension(max(measuredWidth, width), measuredHeight)
127131
}
128132

@@ -131,28 +135,26 @@ class TagLayout : ViewGroup {
131135
var i = 0
132136
for ((childView, rect) in mViewRectMap) {
133137
childView.layout(rect.left, rect.top, rect.right, rect.bottom)
134-
if (onItemClickListener != null || onSingleCheckedChangeListener != null || onMultipleCheckedChangeListener != null) {
135-
childView.isClickable = true
136-
childView.isFocusable = true
137-
val position = i
138-
childView.setOnClickListener {
139-
changedCheckedItemView(position)
140-
if (onItemClickListener != null) {
141-
onItemClickListener?.onItemClick(position, it)
138+
childView.isClickable = true
139+
childView.isFocusable = true
140+
val position = i
141+
childView.setOnClickListener {
142+
changedCheckedItemView(position)
143+
if (onItemClickListener != null) {
144+
onItemClickListener?.onItemClick(position, it)
145+
}
146+
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
147+
if (it.isSelected) {
148+
this.defChoicePosition = position
149+
} else {
150+
this.defChoicePosition = -1
151+
}
152+
if (onSingleCheckedChangeListener != null) {
153+
onSingleCheckedChangeListener?.onCheckedChanged(defChoicePosition)
142154
}
143-
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
144-
if (it.isSelected) {
145-
this.defChoicePosition = position
146-
} else {
147-
this.defChoicePosition = -1
148-
}
149-
if (onSingleCheckedChangeListener != null) {
150-
onSingleCheckedChangeListener?.onCheckedChanged(defChoicePosition)
151-
}
152-
} else if (choiceMode == ChoiceMode.MultipleChoice.choiceMode) {
153-
if (onMultipleCheckedChangeListener != null) {
154-
onMultipleCheckedChangeListener?.onCheckedChanged(getCheckedList())
155-
}
155+
} else if (choiceMode == ChoiceMode.MultipleChoice.choiceMode) {
156+
if (onMultipleCheckedChangeListener != null) {
157+
onMultipleCheckedChangeListener?.onCheckedChanged(getCheckedList())
156158
}
157159
}
158160
}

0 commit comments

Comments
 (0)