Commit c18a8b73 by 霍志良

feat:历史和定位城市添加

parent 84b8fa54
...@@ -16,7 +16,8 @@ import kotlinx.android.synthetic.main.consultant_item_region.view.* ...@@ -16,7 +16,8 @@ import kotlinx.android.synthetic.main.consultant_item_region.view.*
* 省份列表适配器 * 省份列表适配器
* Created by zqk on 17-7-21. * Created by zqk on 17-7-21.
*/ */
class RegionRecyclerViewAdapter(val context: Context, val regionList: ArrayList<RegionItem>, var selectedRegion: RegionItem class RegionRecyclerViewAdapter(
val context: Context, var regionList: ArrayList<RegionItem>, var selectedRegion: RegionItem
) : RecyclerView.Adapter<RegionRecyclerViewAdapter.ViewHolder>() { ) : RecyclerView.Adapter<RegionRecyclerViewAdapter.ViewHolder>() {
var onItemClickListener: MyBaseAdapter.OnItemClickListener<RegionItem>? = null var onItemClickListener: MyBaseAdapter.OnItemClickListener<RegionItem>? = null
......
...@@ -41,6 +41,8 @@ class AreaPopupWindow( ...@@ -41,6 +41,8 @@ class AreaPopupWindow(
private var recentCityBean: ConsultantRecentCity? = ConsultantRecentCity(arrayListOf()) private var recentCityBean: ConsultantRecentCity? = ConsultantRecentCity(arrayListOf())
var onRegionSelectedListener: OnRegionSelectedListener? = null var onRegionSelectedListener: OnRegionSelectedListener? = null
private var subLocationItem: SubItem? = SubItem()
private var locationHistoryList: ArrayList<RegionItem>? = arrayListOf()
init { init {
val view = val view =
...@@ -52,12 +54,34 @@ class AreaPopupWindow( ...@@ -52,12 +54,34 @@ class AreaPopupWindow(
this.isOutsideTouchable = true this.isOutsideTouchable = true
this.height = this.height =
((RxDeviceTool.getScreenHeight(context) - RxImageTool.dp2px(90f)) * 0.618).toInt() //设置高度为屏幕的80% ((RxDeviceTool.getScreenHeight(context) - RxImageTool.dp2px(90f)) * 0.618).toInt() //设置高度为屏幕的80%
//根据IP获取当前城市
getRegionByIp()
recentCityBean =
MMKV.defaultMMKV().decodeParcelable(RECENT_CITY, ConsultantRecentCity::class.java)
if (recentCityBean == null) recentCityBean = ConsultantRecentCity(arrayListOf())
var locationList: ArrayList<SubItem>? = arrayListOf()
locationHistoryList?.add(
RegionItem(
recentCityBean!!.recentCity,
value = "历史",
key = "0",
code = "0"
)
)
subLocationItem?.let { locationList?.add(it) }
locationList?.let { RegionItem(it, value = "定位", key = "-1", code = "-1") }
?.let { locationHistoryList?.add(0, it) }
locationHistoryList?.addAll(regionList)
regionAdapter.regionList = locationHistoryList!!
if (selectedRegion.sub.isNotEmpty()) { if (selectedRegion.sub.isNotEmpty()) {
subList.addAll(selectedRegion.sub) subList.addAll(selectedRegion.sub)
} else { } else {
subList.addAll(regionList[0].sub) subList.addAll(regionList[0].sub)
} }
subAdapter = SubRecyclerViewAdapter(context, subList, selectedSub) subAdapter = SubRecyclerViewAdapter(context, subList, selectedSub)
view.rvRegion.layoutManager = view.rvRegion.layoutManager =
...@@ -74,7 +98,7 @@ class AreaPopupWindow( ...@@ -74,7 +98,7 @@ class AreaPopupWindow(
view.rvSub.scrollToPosition(0) view.rvSub.scrollToPosition(0)
} }
view.rvRegion.adapter = regionAdapter view.rvRegion.adapter = regionAdapter
val i = regionList val i = locationHistoryList!!
.takeWhile { it.key != selectedRegion.key } .takeWhile { it.key != selectedRegion.key }
.count() .count()
view.rvRegion.scrollToPosition(i) view.rvRegion.scrollToPosition(i)
...@@ -87,10 +111,16 @@ class AreaPopupWindow( ...@@ -87,10 +111,16 @@ class AreaPopupWindow(
false false
) )
subAdapter.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data -> subAdapter.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data ->
//保留最近五次的搜索历史记录 //倒序保留最近五次的搜索历史记录并去重
recentCityBean = saveCitySearchRecord(data)
MMKV.defaultMMKV().decodeParcelable(RECENT_CITY, ConsultantRecentCity::class.java) selectedSub = data
if (recentCityBean == null) recentCityBean = ConsultantRecentCity(arrayListOf()) onRegionSelectedListener?.onRegionSelected(selectedRegion, selectedSub)
}
view.rvSub.adapter = subAdapter
view.rvSub.scrollToPosition(subList.indexOf(selectedSub) + 1)
}
private fun saveCitySearchRecord(data: SubItem) {
recentCityBean?.let { recentCityBean?.let {
if (it.recentCity.size < 5) { if (it.recentCity.size < 5) {
it.recentCity.add(0, data) it.recentCity.add(0, data)
...@@ -103,12 +133,6 @@ class AreaPopupWindow( ...@@ -103,12 +133,6 @@ class AreaPopupWindow(
MMKV.defaultMMKV().encode(RECENT_CITY, recentCityBean) MMKV.defaultMMKV().encode(RECENT_CITY, recentCityBean)
} }
} }
selectedSub = data
onRegionSelectedListener?.onRegionSelected(selectedRegion, selectedSub)
}
view.rvSub.adapter = subAdapter
view.rvSub.scrollToPosition(subList.indexOf(selectedSub) + 1)
getRegionByIp()
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
...@@ -118,7 +142,9 @@ class AreaPopupWindow( ...@@ -118,7 +142,9 @@ class AreaPopupWindow(
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe({ .subscribe({
if (it.code == "200") { if (it.code == "200") {
subLocationItem?.code = it.data.city_code
subLocationItem?.key = it.data.city_code
subLocationItem?.value = it.data.city_name
} }
}, {}) }, {})
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment