Android开发列表弹框

效果图:

列表弹框.png

二、思路

列表数据不定,所以采用RecyclerView,创建个回调点击的position位置,处理相应事件即可

二、关键代码

fun showSelectDialog(context: Context,items:MutableList<String>,selectItemListener: SelectItemListener?){
    var dialog = getCancelDialog(context)
    dialog.show()
    val window = dialog.window
    window.setGravity(Gravity.BOTTOM)
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    window.setContentView(R.layout.dialog_items)

    val rv = window.findViewById<RecyclerView>(R.id.rv_items)
    rv.layoutManager = LinearLayoutManager(context)

    val adapter = DialogSelectItemAdapter(data = items)
    rv.adapter = adapter

    adapter.setOnItemClickListener { adapter, view, position ->
        selectItemListener?.clickPosition(position)
        dialog.dismiss()
    }

    val tv_cancel = window.findViewById<TextView>(R.id.tv_cancel)
    tv_cancel.setOnClickListener {
        dialog.dismiss()
    }
}

interface SelectItemListener{
    fun clickPosition(position:Int)
}

推荐阅读更多精彩内容