Skip to content

Commit edfece2

Browse files
committed
优化
1 parent 6a5ae70 commit edfece2

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

app/src/main/java/com/github2136/datalevelpicker_demo/MainActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import com.github2136.datalevelpicker.DataLevelPickerDialog
88
import com.github2136.datalevelpicker.IDataLevel
99

1010
class MainActivity : AppCompatActivity() {
11-
var selectData: MutableList<IDataLevel> = mutableListOf()
11+
var selectData: MutableList<City> = mutableListOf()
1212
val dataLevelPickerDialog by lazy {
13-
val l5 = mutableListOf<IDataLevel>()
13+
val l5 = mutableListOf<City>()
1414
repeat(2) { n ->
1515
val l4 = mutableListOf<City>()
1616
repeat(4) { l ->
@@ -49,10 +49,10 @@ class MainActivity : AppCompatActivity() {
4949
dataLevelPickerDialog.show(supportFragmentManager)
5050
}
5151
R.id.btn2 -> {
52-
if (selectData.isEmpty()) {
52+
if (selectData.isNotEmpty()) {
5353
dataLevelPickerDialog.setData(selectData)
5454
} else {
55-
val d = mutableListOf<IDataLevel>()
55+
val d = mutableListOf<City>()
5656
d.add(City("A0", "", null))
5757
d.add(City("B01", "", null))
5858
d.add(City("C012", "", null))

library/src/main/java/com/github2136/datalevelpicker/DataLevelPickerDialog.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,25 @@ import androidx.recyclerview.widget.RecyclerView
2222
* Created by YB on 2022/12/5
2323
* 级联菜单选择
2424
*/
25-
class DataLevelPickerDialog private constructor() : DialogFragment(), View.OnClickListener {
25+
class DataLevelPickerDialog<T : IDataLevel> private constructor() : DialogFragment(), View.OnClickListener {
2626
private val className by lazy { javaClass.simpleName }
27-
protected lateinit var dataLevel: MutableList<IDataLevel>
27+
private var dataLevel: MutableList<IDataLevel> = mutableListOf()
2828
private var selectData = mutableListOf<IDataLevel>() //选中的集合
2929
private var level = 0 //当前操作等级
30-
private var onConfirm: ((data: MutableList<IDataLevel>) -> Unit)? = null
30+
private var onConfirm: ((data: MutableList<T>) -> Unit)? = null
3131
private lateinit var hsvTitle: HorizontalScrollView
3232
private lateinit var llTitle: LinearLayout
3333
private lateinit var rvList: RecyclerView
3434
private lateinit var btnConfirm: TextView
3535
private lateinit var btnCancel: TextView
3636
private lateinit var adapter: DataLevelPickerAdapter
3737

38-
constructor(data: MutableList<IDataLevel>, onConfirm: (data: MutableList<IDataLevel>) -> Unit) : this() {
39-
dataLevel = data
38+
constructor(data: MutableList<T>, onConfirm: (data: MutableList<T>) -> Unit) : this() {
39+
dataLevel.addAll(data)
4040
this.onConfirm = onConfirm
4141
}
4242

43-
fun setData(data: MutableList<IDataLevel>) {
43+
fun setData(data: MutableList<T>) {
4444
selectData.clear()
4545
level = if (data.isEmpty()) 0 else data.lastIndex
4646
var list = dataLevel
@@ -150,7 +150,7 @@ class DataLevelPickerDialog private constructor() : DialogFragment(), View.OnCli
150150
}
151151
}
152152
R.id.btnConfirm -> {
153-
onConfirm?.invoke(selectData.toMutableList())
153+
onConfirm?.invoke(selectData.toMutableList() as MutableList<T>)
154154
dismiss()
155155
}
156156
R.id.btnCancel -> {

0 commit comments

Comments
 (0)