fix
lmw
2023-03-14 128cbeac95dbc995fe1760bbd0f0a985fa5d23ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.fuban.user.dialog
 
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import cn.sinata.xldutils.activity.BaseActivity
import com.fuban.user.R
import com.fuban.user.network.HttpManager
import com.fuban.user.network.entity.City
import com.fuban.user.network.request
import kotlinx.android.synthetic.main.dialog_region.*
import kotlinx.android.synthetic.main.dialog_single_wheel.tv_cancel
import kotlinx.android.synthetic.main.dialog_single_wheel.tv_sure
import kotlinx.android.synthetic.main.dialog_single_wheel.wv_1
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.wrapContent
 
class RegionDialog : DialogFragment() {
    private val province by lazy {
        arguments?.getSerializable("province") as ArrayList<City>
    }
    private val city= arrayListOf<City>()
    private var isFirst = true //true:第一次显示,需要回显选中的index
 
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_region, container, false)
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME, R.style.Dialog)
    }
 
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.setLayout(matchParent, wrapContent)
        dialog?.window?.setGravity(Gravity.BOTTOM)
        dialog?.setCanceledOnTouchOutside(true)
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        wv_1.setItems(province.map { it.name })
        wv_1.setSeletion(arguments?.getInt("provinceIndex")?:0)
        getRegion()
        wv_1.setOnWheelViewListener { _, _ ->
            getRegion()
        }
        wv_2.setOnWheelViewListener { _, _ ->
            getRegion(false)
        }
 
        tv_cancel.setOnClickListener {
            dismiss()
        }
        tv_sure.setOnClickListener {
            callback?.onOk(wv_1.seletedItem+wv_2.seletedItem+wv_3.seletedItem,wv_1.seletedIndex,wv_2.seletedIndex,wv_3.seletedIndex)
            dismiss()
        }
    }
 
    /**
     * @param isCity true:城市 false:地区
     */
    private fun getRegion(isCity:Boolean = true){
        if ((!isCity&&city.isEmpty())||!isAdded)
            return
        HttpManager.queryRegions(if (isCity) province[wv_1.seletedIndex].id else city[wv_2.seletedIndex].id).request(activity as BaseActivity){_,data->
            data?.apply {
                if (isCity){
                    city.clear()
                    city.addAll(this)
                    wv_2?.setItems(city.map { it.name })
                    if (isFirst)
                        wv_2?.setSeletion(arguments?.getInt("cityIndex")?:0)
                    else
                        wv_2?.setSeletion(0)
                    if (city.isNotEmpty())
                        getRegion(false)
                    else{
                        wv_3?.setItems(arrayListOf())
                        wv_3?.setSeletion(0)
                    }
                }else{
                    wv_3?.setItems(map{it.name})
                    if (isFirst)
                        wv_3?.setSeletion(arguments?.getInt("regionIndex")?:0)
                    else
                        wv_3?.setSeletion(0)
                }
            }
        }
    }
 
    interface OnOkClick{
        fun onOk(region:String,provinceIndex:Int,cityIndex:Int,regionIndex:Int)
    }
 
    private var callback: OnOkClick? = null
 
    fun setCallback(callback: OnOkClick) {
        this.callback = callback
    }
}