AreaPopupWindow.kt 3.25 KB
Newer Older
1 2 3 4
package com.yidianling.consultant.ui.view

import android.content.Context
import android.graphics.drawable.BitmapDrawable
YKai committed
5 6
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSnapHelper
7 8 9 10 11 12 13 14 15 16 17
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import com.ydl.ydlcommon.adapter.MyBaseAdapter
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
import com.yidianling.consultant.R
import com.yidianling.consultant.adapter.RegionRecyclerViewAdapter
import com.yidianling.consultant.adapter.SubRecyclerViewAdapter
import com.yidianling.consultant.model.bean.RegionItem
import com.yidianling.consultant.model.bean.SubItem
18
import kotlinx.android.synthetic.main.consultant_ui_region_popup_window.view.*
19 20 21 22

/**
 * 地区选择弹窗
 */
23
class AreaPopupWindow(val context: Context, regionList: ArrayList<RegionItem>, private var selectedRegion: RegionItem, private var selectedSub: SubItem)
24 25 26 27
    : PopupWindow(null, ViewGroup.LayoutParams.MATCH_PARENT, RxImageTool.dp2px(369f)) {

    private val subList = ArrayList<SubItem>()
    private val regionAdapter = RegionRecyclerViewAdapter(context, regionList, selectedRegion)
28
    private var subAdapter:SubRecyclerViewAdapter
29 30 31 32

    var onRegionSelectedListener: OnRegionSelectedListener? = null

    init {
33
        val view = LayoutInflater.from(context).inflate(R.layout.consultant_ui_region_popup_window, null)
34 35 36 37 38 39 40
        this.contentView = view
        this.isFocusable = true
        @Suppress("DEPRECATION")
        this.setBackgroundDrawable(BitmapDrawable())
        this.isOutsideTouchable = true
        this.height = ((RxDeviceTool.getScreenHeight(context) - RxImageTool.dp2px(90f)) * 0.618).toInt() //设置高度为屏幕的80%

41 42 43 44 45 46 47
        if (selectedRegion.sub.isNotEmpty()){
            subList.addAll(selectedRegion.sub)
        }else{
            subList.addAll(regionList[0].sub)
        }
        subAdapter  = SubRecyclerViewAdapter(context, subList, selectedSub)

YKai committed
48 49 50 51 52 53
        view.rvRegion.layoutManager =
            LinearLayoutManager(
                context,
                LinearLayoutManager.VERTICAL,
                false
            )
54 55 56 57 58 59 60 61 62 63 64
        regionAdapter.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data ->
            selectedRegion = data
            subList.clear()
            subList.addAll(data.sub)
            subAdapter.notifyDataSetChanged()
            view.rvSub.scrollToPosition(0)
        }
        view.rvRegion.adapter = regionAdapter
        val i = regionList
                .takeWhile { it.key != selectedRegion.key }
                .count()
65
        view.rvRegion.scrollToPosition(i)
66 67


YKai committed
68 69 70 71 72 73
        view.rvSub.layoutManager =
            LinearLayoutManager(
                context,
                LinearLayoutManager.VERTICAL,
                false
            )
74 75 76 77 78 79 80 81 82 83 84 85
        subAdapter.onItemClickListener = MyBaseAdapter.OnItemClickListener { _, _, data ->
            selectedSub = data
            onRegionSelectedListener?.onRegionSelected(selectedRegion, selectedSub)
        }
        view.rvSub.adapter = subAdapter
        view.rvSub.scrollToPosition(subList.indexOf(selectedSub) + 1)
    }

    interface OnRegionSelectedListener {
        fun onRegionSelected(region: RegionItem, sub: SubItem)
    }
}