Skip to content

Commit 30ed67c

Browse files
author
杨宏伟
committed
优化标签点击事件
1 parent 52e5e98 commit 30ed67c

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ allprojects {
1212
}
1313

1414
dependencies {
15-
implementation 'com.github.LuckyCodeer:TagLayout:1.0.3'
15+
implementation 'com.github.LuckyCodeer:TagLayout:1.0.4'
1616
}
1717
```
1818

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
77
import android.widget.TextView
8+
import android.widget.Toast
89
import androidx.appcompat.app.AppCompatActivity
910
import androidx.recyclerview.widget.RecyclerView
1011
import androidx.recyclerview.widget.RecyclerView.Adapter
@@ -77,6 +78,15 @@ class ListActivity : AppCompatActivity() {
7778
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
7879
holder.textView.text = list[position].name
7980
holder.tagLayout.setAdapter(MyTagAdapter(list[position].tagList))
81+
holder.itemView.setOnClickListener {
82+
Toast.makeText(holder.itemView.context, "" + position, Toast.LENGTH_SHORT).show()
83+
}
84+
/*holder.tagLayout.onItemClickListener = object : TagLayout.OnItemClickListener {
85+
override fun onItemClick(position: Int, view: View) {
86+
Toast.makeText(holder.itemView.context, "点击了标签$position", Toast.LENGTH_SHORT)
87+
.show()
88+
}
89+
}*/
8090
}
8191

8292
}
@@ -93,6 +103,7 @@ class ListActivity : AppCompatActivity() {
93103
}
94104

95105
override fun onBindView(itemView: View, position: Int) {
106+
itemView.setBackgroundResource(R.drawable.tag_normal_bg)
96107
val textView: TextView = itemView.findViewById(R.id.tv_title)
97108
textView.text = dataList[position]
98109
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
android:layout_width="wrap_content"
44
android:layout_height="wrap_content"
55
android:layout_margin="10dp"
6-
android:clickable="true"
7-
android:focusable="true"
86
android:background="@drawable/tag_selector_bg"
97
android:orientation="vertical">
108

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 16
1111
targetSdkVersion 30
12-
versionCode 103
13-
versionName "1.0.3"
12+
versionCode 104
13+
versionName "1.0.4"
1414

1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
consumerProguardFiles "consumer-rules.pro"

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

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import android.graphics.Rect
66
import android.util.AttributeSet
77
import android.util.Log
88
import android.view.View
9+
import android.view.View.OnLongClickListener
910
import android.view.ViewGroup
1011
import kotlin.math.max
1112

1213
private const val TAG = "TagLayout"
1314

1415
/**
1516
* 标签布局
17+
* @author yhw
1618
*/
1719
class TagLayout : ViewGroup {
1820
private val mViewRectList = mutableListOf<Rect>()
@@ -82,8 +84,6 @@ class TagLayout : ViewGroup {
8284
}
8385

8486
if (!mChildViewList.contains(childView)) {
85-
childView.isClickable = true
86-
childView.isFocusable = true
8787
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
8888
if (i in 0 until childCount && i == defChoicePosition)
8989
childView.isSelected = true
@@ -101,28 +101,34 @@ class TagLayout : ViewGroup {
101101
)
102102
mViewRectList.add(rect)
103103

104-
childView.setOnClickListener {
105-
changedCheckedItemView(i)
106-
if (onItemClickListener != null) {
107-
onItemClickListener?.onItemClick(i, it)
108-
}
109-
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
110-
this.defChoicePosition = i
111-
if (onSingleCheckedChangeListener != null) {
112-
onSingleCheckedChangeListener?.onCheckedChanged(defChoicePosition)
104+
if (onItemClickListener != null || onSingleCheckedChangeListener != null || onMultipleCheckedChangeListener != null) {
105+
childView.isClickable = true
106+
childView.isFocusable = true
107+
childView.setOnClickListener {
108+
changedCheckedItemView(i)
109+
if (onItemClickListener != null) {
110+
onItemClickListener?.onItemClick(i, it)
113111
}
114-
} else if (choiceMode == ChoiceMode.MultipleChoice.choiceMode) {
115-
if (onMultipleCheckedChangeListener != null) {
116-
onMultipleCheckedChangeListener?.onCheckedChanged(getCheckedList())
112+
if (choiceMode == ChoiceMode.SingleChoice.choiceMode) {
113+
this.defChoicePosition = i
114+
if (onSingleCheckedChangeListener != null) {
115+
onSingleCheckedChangeListener?.onCheckedChanged(defChoicePosition)
116+
}
117+
} else if (choiceMode == ChoiceMode.MultipleChoice.choiceMode) {
118+
if (onMultipleCheckedChangeListener != null) {
119+
onMultipleCheckedChangeListener?.onCheckedChanged(getCheckedList())
120+
}
117121
}
118122
}
119123
}
120124

121-
childView.setOnLongClickListener {
122-
if (onItemLongClickListener != null) {
123-
onItemLongClickListener?.onItemLongClick(i, it)
124-
}
125-
true
125+
if (onItemLongClickListener != null) {
126+
childView.isClickable = true
127+
childView.isFocusable = true
128+
childView.setOnLongClickListener(OnLongClickListener { v ->
129+
onItemLongClickListener?.onItemLongClick(i, v)
130+
true
131+
})
126132
}
127133
}
128134

0 commit comments

Comments
 (0)