Android Kotlin仿iOS底部选择框

先看下使用方式:

DialogFunction(
    data = listOf(
        "相册",
        "拍照"
    )
) { position, _ ->
    setListener(baseActivity)
    when (position) {
        0 -> {//选择相册
        }
        1 -> {//拍照
        }
    }
}.show(activity.supportFragmentManager, "showPhotoDialog")

源码:继承自DialogFragment

/**
 * 功能选择弹窗
 * 比如:拍照/选择照片
 *
 */
class DialogFunction(
    private val data: List,
    private val itemClickListener: (position: Int, bean: String) -> Unit
) : DialogFragment() {

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setGravity(Gravity.BOTTOM)
        }
    }

    override fun show(manager: FragmentManager, tag: String?) {
        val t = manager.beginTransaction()
        val f = manager.findFragmentByTag(tag)
        if (f != null) {
            t.remove(f)
        }
        t.commit()
        super.show(manager, tag)

    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_function, container, false)
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val tvFuncCancel = view.findViewById(R.id.tv_func_cancel)
        val recyclerView = view.findViewById(R.id.rv_func)

        tvFuncCancel.setOnClickListener {
            dismiss()
        }

        val divider = LinearDecoration(
            this.requireContext(),
            DividerItemDecoration.VERTICAL
        )
        view.context.resources.getDrawable(R.drawable.shape_divider,null)?.let { divider.setDrawable(it) }
        recyclerView.addItemDecoration(
            divider
        )
        recyclerView.adapter = FuncAdapter(data) { position, bean ->
            itemClickListener.invoke(position, bean)
            dismiss()
        }

    }
}

class FuncAdapter(
    private val data: List,
    private val itemClickListener: (position: Int, bean: String) -> Unit
) : RecyclerView.Adapter() {
    inner class FuncVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tv = itemView.findViewById(R.id.tv_item_func)
        fun bind(s: String, position: Int) {
            tv.text = s
            tv.setOnClickListener {
                itemClickListener.invoke(position, s)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FuncVH {
        return FuncVH(
            LayoutInflater.from(parent.context).inflate(R.layout.item_dialog_func, parent, false)
        )
    }

    override fun onBindViewHolder(holder: FuncVH, position: Int) {
        holder.bind(data[position], position)
    }

    override fun getItemCount(): Int {
        return data.size
    }
}

分割线:shape_divider.xml




    
    


布局:dialog_function.xml





    

    


子布局:item_dialog_func.xml




    

你可能感兴趣的:(自定义控件,Android,android,kotlin,ios)