Commit c18a8b73 by 霍志良

feat:历史和定位城市添加

parent 84b8fa54
......@@ -16,7 +16,8 @@ import kotlinx.android.synthetic.main.consultant_item_region.view.*
* 省份列表适配器
* 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>() {
var onItemClickListener: MyBaseAdapter.OnItemClickListener<RegionItem>? = null
......
......@@ -41,6 +41,8 @@ class AreaPopupWindow(
private var recentCityBean: ConsultantRecentCity? = ConsultantRecentCity(arrayListOf())
var onRegionSelectedListener: OnRegionSelectedListener? = null
private var subLocationItem: SubItem? = SubItem()
private var locationHistoryList: ArrayList<RegionItem>? = arrayListOf()
init {
val view =
......@@ -52,12 +54,34 @@ class AreaPopupWindow(
this.isOutsideTouchable = true
this.height =
((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()) {
subList.addAll(selectedRegion.sub)
} else {
subList.addAll(regionList[0].sub)
}
subAdapter = SubRecyclerViewAdapter(context, subList, selectedSub)
view.rvRegion.layoutManager =
......@@ -74,7 +98,7 @@ class AreaPopupWindow(
view.rvSub.scrollToPosition(0)
}
view.rvRegion.adapter = regionAdapter
val i = regionList
val i = locationHistoryList!!
.takeWhile { it.key != selectedRegion.key }
.count()
view.rvRegion.scrollToPosition(i)
......@@ -87,10 +111,16 @@ class AreaPopupWindow(
false
)
subAdapter.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data ->
//保留最近五次的搜索历史记录
recentCityBean =
MMKV.defaultMMKV().decodeParcelable(RECENT_CITY, ConsultantRecentCity::class.java)
if (recentCityBean == null) recentCityBean = ConsultantRecentCity(arrayListOf())
//倒序保留最近五次的搜索历史记录并去重
saveCitySearchRecord(data)
selectedSub = data
onRegionSelectedListener?.onRegionSelected(selectedRegion, selectedSub)
}
view.rvSub.adapter = subAdapter
view.rvSub.scrollToPosition(subList.indexOf(selectedSub) + 1)
}
private fun saveCitySearchRecord(data: SubItem) {
recentCityBean?.let {
if (it.recentCity.size < 5) {
it.recentCity.add(0, data)
......@@ -103,12 +133,6 @@ class AreaPopupWindow(
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")
......@@ -118,7 +142,9 @@ class AreaPopupWindow(
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
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