Skip to content

Commit 7ff2303

Browse files
committed
单选支持取消选中
1 parent 732b53e commit 7ff2303

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ Android上的标签布局、流式布局,支持单选、多选等操作
33

44
### 引入依赖
55

6-
```kotlin
6+
```groovy
77
allprojects {
88
repositories {
9-
...
109
maven { url 'https://jitpack.io' }
1110
}
1211
}
1312
1413
dependencies {
15-
implementation 'com.github.LuckyCodeer:TagLayout:1.0.5'
14+
implementation 'com.github.LuckyCodeer:TagLayout:1.0.6'
1615
}
1716
```
1817

@@ -61,6 +60,7 @@ dependencies {
6160
##### 布局属性
6261
1. app:defaultChoicePosition="0" //单选时默认选中项
6362
2. app:choiceMode="none" //设置选择模式,支持单选(singleChoice)和多选(multipleChoice) 默认(none)表示不设置选择模式
63+
3. app:singleChoiceSupportCancel="false" //单选支持取消已选中项,默认为false
6464

6565

6666
#### 动态添加数据
@@ -102,6 +102,7 @@ tagLayout2.onMultipleCheckedChangeListener =
102102
//获取选中项
103103
getIndexBtn.setOnClickListener{
104104
if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.SingleChoice.choiceMode){
105+
//tagLayout2.getCheckedPosition() 返回-1代表未选中任何选项
105106
Toast.makeText(this@MainActivity, "单选了 ${tagLayout2.getCheckedPosition()}", Toast.LENGTH_SHORT).show()
106107
}else if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.MultipleChoice.choiceMode){
107108
Toast.makeText(this@MainActivity, "多选了 ${tagLayout2.getCheckedList()}", Toast.LENGTH_SHORT).show()

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@
3636
android:background="@drawable/tag_selector_bg"
3737
android:padding="10dp"
3838
android:text="多选" />
39+
3940
</com.yhw.taglayout.TagLayout>
4041

4142
<com.yhw.taglayout.TagLayout
4243
android:id="@+id/tag_layout_2"
4344
android:layout_width="match_parent"
4445
android:layout_height="wrap_content"
4546
app:choiceMode="none"
46-
app:defaultChoicePosition="0" />
47+
app:defaultChoicePosition="0"
48+
app:singleChoiceSupportCancel="true"/>
4749

4850
<LinearLayout
4951
android:layout_width="match_parent"

build.gradle

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
buildscript {
3-
ext.kotlin_version = "1.4.31"
3+
ext {
4+
agp_version = '7.1.2'
5+
}
6+
ext.kotlin_version = "1.6.10"
47
repositories {
8+
maven { url 'https://maven.aliyun.com/repository/public/' }
9+
maven { url 'https://maven.aliyun.com/repository/google/' }
510
google()
611
mavenCentral()
712
maven { url 'https://jitpack.io' }
813
}
914
dependencies {
10-
classpath "com.android.tools.build:gradle:4.1.2"
15+
classpath "com.android.tools.build:gradle:$agp_version"
1116
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1217
}
1318
}
1419

1520
allprojects {
1621
repositories {
22+
maven { url 'https://maven.aliyun.com/repository/public/' }
23+
maven { url 'https://maven.aliyun.com/repository/google/' }
1724
google()
1825
mavenCentral()
1926
maven { url 'https://jitpack.io' }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Thu Mar 18 16:31:36 CST 2021
1+
#Wed Sep 06 15:10:03 CST 2023
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
45
zipStoreBase=GRADLE_USER_HOME
56
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

library/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212

1313
<!--单选时默认选择项-->
1414
<attr name="defaultChoicePosition" format="integer"/>
15+
<!--单选支持取消-->
16+
<attr name="singleChoiceSupportCancel" format="boolean"/>
1517
</declare-styleable>
1618
</resources>

0 commit comments

Comments
 (0)