Android Kotlin仿iOS底部选择框

先看下使用方式:

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

源码:继承自DialogFragment

/**
 * 功能选择弹窗
 * 比如:拍照/选择照片
 *
 */
class DialogFunction(
    private val data: List<String>,
    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<TextView>(R.id.tv_func_cancel)
        val recyclerView = view.findViewById<RecyclerView>(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<String>,
    private val itemClickListener: (position: Int, bean: String) -> Unit
) : RecyclerView.Adapter<FuncAdapter.FuncVH>() {
    inner class FuncVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tv = itemView.findViewById<TextView>(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

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#1AFFFFFF" />
    <size
        android:width="3dp"
        android:height="0.5dp" />

</shape>

布局:dialog_function.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:orientation="vertical"
    tools:viewBindingIgnore="true">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_func"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_corner14_gray1"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:itemCount="8"
        tools:listitem="@layout/item_dialog_func" />

    <TextView
        android:id="@+id/tv_func_cancel"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/shape_corner14_gray2"
        android:gravity="center"
        android:text="取消"
        android:textColor="@color/theme_main_color"
        android:textSize="20sp" />

</LinearLayout>

子布局:item_dialog_func.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_item_func"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#ffffff"
        android:textSize="20sp"
        tools:text="man" />

</LinearLayout>

上一篇:通过MAC地址,自动判定生产环境、测试环境


下一篇:安卓——家庭记账本7