安卓开发

使用kotlin自定义dialog详解

2020-04-10  本文已影响0人  夜封雪_2696

```

/**

*  author:CQ

*  Date:2020-04-09

*  Description:调拨出入库类型选择弹出框

*/

class AllotTypeDialog : Dialog {

    private var cox: Context? =null

    private var allotTitle: TextView? =null// 标题

    private var allotRecyclerView: RecyclerView? =null // 列表

    private var allotSure: TextView? =null // 确定按钮

    private var allotTypeDialogAdapter: AllotTypeDialogAdapter? =null // 适配器

    private var list: MutableList? =null// list

    private var onSelectTypeListener: OnSelectTypeListener? =null // 选择类型监听

    private var selectIn: String =""

    public fun SetOnSelectTypeListener(onSelectTypeListener: OnSelectTypeListener) {

        this.onSelectTypeListener = onSelectTypeListener

    }

    constructor(context: Context) :super(context) {

            cox = context

            initView()

        }

      constructor(context: Context, themeStyle: Int, select: String) :super(context, themeStyle) {

            cox = context

            this.selectIn = select

            initView()

    }

    private fun initView() {

list =mutableListOf()

onInitData()

}

private fun onInitData() {

val selectList =selectIn.split(",")

list!!.run {

            add(AllotTypeBean(0, "全部", selectList.size ==9))

add(AllotTypeBean(1, "渠道调拨", selectList.contains("1")))

add(AllotTypeBean(2, "渠道调退", selectList.contains("2")))

add(AllotTypeBean(3, "店铺配货", selectList.contains("3")))

add(AllotTypeBean(4, "店铺配退", selectList.contains("4")))

add(AllotTypeBean(5, "批发销货", selectList.contains("5")))

add(AllotTypeBean(6, "批发退货", selectList.contains("6")))

add(AllotTypeBean(9, "仓库移仓", selectList.contains("9")))

add(AllotTypeBean(10, "门店横调", selectList.contains("10")))

add(AllotTypeBean(30, "越级调拨", selectList.contains("30")))

}

    }

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

var dialogWindow =window

        dialogWindow.setGravity(Gravity.CENTER)

setContentView(R.layout.dialog_allot_type)

allotTitle = findViewById(R.id.dialog_allot_type_title)

allotRecyclerView = findViewById(R.id.dialog_allot_type_recycleView)

allotSure = findViewById(R.id.dialog_allot_type_sure)

allotSure!!.setOnClickListener{

            var select =""

            for (iin 1 until list!!.size) {

if (list!![i].isCheck)

select +="${list!![i].type},"

            }

if (select =="") {

(cox as AllotAbsActivity).toastMsg("请选择类型")

}else {

select = select.substring(0, select.length -1)

if (onSelectTypeListener !=null) {

onSelectTypeListener!!.onSelect(select)

}

dismiss()

}

}

        allotTypeDialogAdapter = AllotTypeDialogAdapter(cox!!, list!!)

allotRecyclerView!!.layoutManager = LinearLayoutManager(cox)

allotRecyclerView!!.adapter =allotTypeDialogAdapter

        allotTypeDialogAdapter!!.setOnClickItemListener(object : AllotTypeDialogAdapter.OnClickItemListener {

override fun onClickItem(position: Int) {

if (position ==0) {

list!![0].isCheck = !list!![0].isCheck

                    for (iin 1 until list!!.size) {

list!![i].isCheck =list!![0].isCheck

                    }

}else {

list!![position].isCheck = !list!![position].isCheck

                    var selectAll =true

                    for (iin 1 until list!!.size) {

if (!list!![i].isCheck)

selectAll =false

                    }

list!![0].isCheck = selectAll

}

allotTypeDialogAdapter!!.setOnRefresh(list!!)

}

})

var windowManager = (cox as Activity).windowManager

        var display = windowManager.defaultDisplay

        var lp =window.attributes

        lp.width = display.width *4 /5

        window.attributes = lp

setCanceledOnTouchOutside(true)

}

/**

* 获取显示类型

*/

    public fun getTypeShow(): String {

var show =""

        for (iin 1 until list!!.size) {

if (list!![i].isCheck)

show +="${list!![i].name},"

        }

if (show =="")

return show

return show.substring(0, show.length -1)

}

public interface OnSelectTypeListener {

fun onSelect(select: String)

}

}

上一篇 下一篇

猜你喜欢

热点阅读