lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
package com.fuban.user.ui
 
import android.app.Activity
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
import com.fuban.user.FBApplication
import com.fuban.user.R
import com.fuban.user.network.HttpManager
import com.fuban.user.network.entity.OpenCity
import com.fuban.user.network.request
import com.fuban.user.ui.trip.adapter.OpenCityAdapter
import kotlinx.android.synthetic.main.activity_choose_city.*
 
class ChooseCityActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_choose_city
 
    private val changeRoot by lazy { //true:改变全局选择城市,false:只改变当前功能城市(比如地址搜索)
        intent.getBooleanExtra("changeRoot",true)
    }
    private val index = arrayListOf(
        "A", "B", "C", "D", "E", "F", "G", "H", "I",
        "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"
    )
    private val datasAll = arrayListOf<OpenCity>() //所有数据源
    private val datas = arrayListOf<OpenCity>() //展示数据源(搜索后过滤)
    private val adapter = OpenCityAdapter(datas)
    override fun initClick() {
        tv_cancel.setOnClickListener {
            finish()
        }
 
        et_search.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                hideInputMethod()
                val key = et_search.text.toString()
                datas.clear()
                datasAll.forEach {
                    if (it.name.contains(key)){
                        datas.add(it)
                    }
                }
                adapter.notifyDataSetChanged()
                if (datas.isEmpty())
                    lv_city.setLoadMoreText("没有搜索结果")
                else
                    lv_city.setLoadMoreText("")
            }
            return@setOnEditorActionListener false
        }
 
        adapter.setOnItemClickListener { _, position ->
            if (!changeRoot){
                setResult(Activity.RESULT_OK,intent.putExtra("name" , datas[position].name))
                finish()
            }else{
                FBApplication.chooseCityName = datas[position].name
                FBApplication.chooseCityCode = datas[position].content
                showDialog()
                HttpManager.queryBusinessById(datas[position].id).request(this){_,data->
                    data?.let {
                        setResult(Activity.RESULT_OK,intent.putExtra("func",data)
                            .putExtra("lat" , datas[position].lat).putExtra("lon" , datas[position].lon))
                        finish()
                    }
                }
            }
        }
    }
 
    override fun initView() {
        title = "切换城市"
        tv_current.text = String.format("当前选择:%s",FBApplication.chooseCityName)
        lv_city.setLayoutManager(LinearLayoutManager(this))
        lv_city.setMode(SwipeRefreshRecyclerLayout.Mode.None)
        lv_city.setAdapter(adapter)
        side_bar.setOnSelectIndexItemListener { index ->
            (0 until datas.size).forEach {
                if (index == datas[it].getInitial()) {
                    val manager = lv_city.mRecyclerView.layoutManager as LinearLayoutManager
                    manager.scrollToPositionWithOffset(it, 0)
                    return@setOnSelectIndexItemListener
                }
            }
        }
        getData()
    }
 
    private fun getData(){
        HttpManager.queryOpenCity().request(this){_,data->
            data?.let {
                datas.clear()
                if (it.isNotEmpty()) {
                    index.forEach { index->
                        it.filter { it.name.isNotEmpty() }.forEach {
                            if (index == it.getInitial()){
                                datasAll.add(it)
                            }
                        }
                    }
                }else
                    lv_city.setLoadMoreText("暂无数据")
                datas.addAll(datasAll)
                adapter.notifyDataSetChanged()
            }
        }
    }
}