Skip to content

Commit d55743a

Browse files
committed
增加注释,修改说明
1 parent fe5f4d9 commit d55743a

File tree

5 files changed

+77
-40
lines changed

5 files changed

+77
-40
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Android上的标签布局、流式布局,支持单选、多选等操作
44
### 引入依赖
55

66
```kotlin
7-
implementation 'com.yhw.library:taglayout:1.0.0'
7+
implementation 'com.yhw.library:taglayout:1.0.1'
88
```
99

1010
### 使用方法
@@ -49,8 +49,9 @@ implementation 'com.yhw.library:taglayout:1.0.0'
4949
app:choiceMode="none" />
5050
```
5151

52-
app:defaultChoicePosition="0"<!--单选时默认选中项-->
53-
app:choiceMode="none" /><!--设置选择模式,支持单选(singleChoice)和多选(multipleChoice) 默认(none)表示不设置选择模式-->
52+
##### 布局属性
53+
*app:defaultChoicePosition="0" //单选时默认选中项*
54+
*app:choiceMode="none" //设置选择模式,支持单选(singleChoice)和多选(multipleChoice) 默认(none)表示不设置选择模式*
5455

5556

5657
#### 动态添加数据

app/src/main/java/com/yhw/taglayout/sample/MainActivity.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,20 @@ class MainActivity : AppCompatActivity() {
6565
}
6666

6767
//获取选中项
68-
getIndexBtn.setOnClickListener{
69-
if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.SingleChoice.choiceMode){
70-
Toast.makeText(this@MainActivity, "单选了 ${tagLayout2.getCheckedPosition()}", Toast.LENGTH_SHORT).show()
71-
}else if(tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.MultipleChoice.choiceMode){
72-
Toast.makeText(this@MainActivity, "多选了 ${tagLayout2.getCheckedList()}", Toast.LENGTH_SHORT).show()
73-
}else{
68+
getIndexBtn.setOnClickListener {
69+
if (tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.SingleChoice.choiceMode) {
70+
Toast.makeText(
71+
this@MainActivity,
72+
"单选了 ${tagLayout2.getCheckedPosition()}",
73+
Toast.LENGTH_SHORT
74+
).show()
75+
} else if (tagLayout2.getChoiceMode() == TagLayout.ChoiceMode.MultipleChoice.choiceMode) {
76+
Toast.makeText(
77+
this@MainActivity,
78+
"多选了 ${tagLayout2.getCheckedList()}",
79+
Toast.LENGTH_SHORT
80+
).show()
81+
} else {
7482
Toast.makeText(this@MainActivity, "非选中模式,不能获取", Toast.LENGTH_SHORT).show()
7583
}
7684
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@
5757

5858
</LinearLayout>
5959

60+
6061
</LinearLayout>

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ publish {
77
userOrg = 'yanghongwei'
88
groupId = 'com.yhw.library'
99
artifactId = 'taglayout'
10-
publishVersion = '1.0.0'
10+
publishVersion = '1.0.1'
1111
repoName="TagLayout"
1212
desc = 'Android TagLayout'
1313
website = 'https://github.com/LuckyCodeer/TagLayout'

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

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class TagLayout : ViewGroup {
8585
if (i in 0 until childCount && i == defChoicePosition)
8686
childView.isSelected = true
8787
}
88-
if(childView.background == null){
88+
if (childView.background == null) {
8989
childView.setBackgroundResource(R.drawable.tag_selector_bg)
9090
}
9191
mChildViewList.add(childView)
@@ -146,6 +146,9 @@ class TagLayout : ViewGroup {
146146
}
147147
}
148148

149+
/**
150+
* 改变选中
151+
*/
149152
private fun changedCheckedItemView(position: Int) {
150153
Log.i(TAG, "choiceMode is $choiceMode")
151154
for ((index, view) in mChildViewList.withIndex()) {
@@ -185,38 +188,16 @@ class TagLayout : ViewGroup {
185188
return defChoicePosition
186189
}
187190

188-
override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
189-
return MarginLayoutParams(context, attrs)
190-
}
191-
192-
override fun generateDefaultLayoutParams(): LayoutParams {
193-
return MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
194-
}
195-
196-
override fun generateLayoutParams(p: LayoutParams?): LayoutParams {
197-
return MarginLayoutParams(p)
198-
}
199-
200-
interface OnItemClickListener {
201-
fun onItemClick(position: Int, view: View)
202-
}
203-
204-
interface OnItemLongClickListener {
205-
fun onItemLongClick(position: Int, view: View)
206-
}
207-
208-
interface OnSingleCheckedChangeListener {
209-
fun onCheckedChanged(position: Int)
210-
}
211-
212-
interface OnMultipleCheckedChangeListener {
213-
fun onCheckedChanged(positionList: MutableList<Int>)
214-
}
215-
191+
/**
192+
* 设置数据适配器
193+
*/
216194
fun setAdapter(adapter: TagAdapter) {
217195
mAdapter = adapter
196+
changedAdapter()
197+
}
198+
199+
private fun changedAdapter() {
218200
removeAllViews()
219-
Log.i(TAG, "count ${mAdapter.getItemCount()}")
220201
for (i in 0 until mAdapter.getItemCount()) {
221202
val itemView = mAdapter.onCreateView(this)
222203
mAdapter.onBindView(itemView, i)
@@ -228,6 +209,10 @@ class TagLayout : ViewGroup {
228209
return mAdapter
229210
}
230211

212+
/**
213+
* 设置选择模式
214+
* @see ChoiceMode
215+
*/
231216
fun setChoiceMode(mode: ChoiceMode) {
232217
this.choiceMode = mode.choiceMode
233218
if (mode.choiceMode == ChoiceMode.SingleChoice.choiceMode) {
@@ -241,6 +226,9 @@ class TagLayout : ViewGroup {
241226
}
242227
}
243228

229+
/**
230+
* 单选模式下,设置默认选择项
231+
*/
244232
fun setDefaultChoicePosition(position: Int) {
245233
this.defChoicePosition = position
246234
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
@@ -252,9 +240,48 @@ class TagLayout : ViewGroup {
252240
return choiceMode
253241
}
254242

243+
override fun generateLayoutParams(attrs: AttributeSet?): LayoutParams {
244+
return MarginLayoutParams(context, attrs)
245+
}
246+
247+
override fun generateDefaultLayoutParams(): LayoutParams {
248+
return MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
249+
}
250+
251+
override fun generateLayoutParams(p: LayoutParams?): LayoutParams {
252+
return MarginLayoutParams(p)
253+
}
254+
255+
interface OnItemClickListener {
256+
fun onItemClick(position: Int, view: View)
257+
}
258+
259+
interface OnItemLongClickListener {
260+
fun onItemLongClick(position: Int, view: View)
261+
}
262+
263+
interface OnSingleCheckedChangeListener {
264+
fun onCheckedChanged(position: Int)
265+
}
266+
267+
interface OnMultipleCheckedChangeListener {
268+
fun onCheckedChanged(positionList: MutableList<Int>)
269+
}
270+
255271
enum class ChoiceMode(var choiceMode: Int) {
272+
/**
273+
* 非选择模式
274+
*/
256275
None(0),
276+
277+
/**
278+
* 单选模式
279+
*/
257280
SingleChoice(1),
281+
282+
/**
283+
* 多选模式
284+
*/
258285
MultipleChoice(2);
259286
}
260287
}

0 commit comments

Comments
 (0)