Kotlin 中BottomSheetDialog中禁止上下拖拽

2023-09-03  本文已影响0人  因为我的心

一、前言:

在开发中,我们遇到有些弹窗不需要上下拖拽的功能,也不需要点击阴影取消掉弹窗,可以使用以下方式实现。

效果图.png

二、代码实现

1、BottomSheetDialog弹窗代码


    /**
     * 举报弹窗
     */
    var mBottomSheetReportDialog: BottomSheetDialog? = null
    inline fun recommendBookInfo(context: Activity, noinline result: (Boolean) -> Unit) {
        try {
            if (ClickUtils.isFastDoubleClick400()) {
                if (mBottomSheetReportDialog == null) {
                    mBottomSheetReportDialog = BottomSheetDialog(context)
                    val bottomSheetView: View =
                        View.inflate(context, R.layout.dialog_bottom_recommend_book, null)
                    mBottomSheetReportDialog?.setContentView(bottomSheetView)
                    mBottomSheetReportDialog?.window?.findViewById<View>(R.id.design_bottom_sheet)
                        ?.setBackgroundColor(Color.TRANSPARENT)

                    // 设置点击阴影不取消对话框
                    mBottomSheetReportDialog?.setCanceledOnTouchOutside(false)

                    // 获取 BottomSheetBehavior
                    val behavior = BottomSheetBehavior.from(bottomSheetView.parent as View)
                    //设置展开模式
                    behavior.state = BottomSheetBehavior.STATE_EXPANDED
                    behavior.setBottomSheetCallback(object :
                        BottomSheetBehavior.BottomSheetCallback() {
                        override fun onStateChanged(bottomSheet: View, newState: Int) {
                            //设置展开模式
                            behavior.state = BottomSheetBehavior.STATE_EXPANDED
//                            if (newState == BottomSheetBehavior.STATE_EXPANDED) {
//                                 //Log.e("BottomSheet", "Expanded");
//                            } else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
//                                 //Log.e("BottomSheet", "Collapsed");
//                             }
                        }

                        override fun onSlide(bottomSheet: View, slideOffset: Float) {
                        }
                    })

                    //获取控件点击
                    val tvReport = bottomSheetView.findViewById<TextView>(R.id.tv_report)
                    val tvCancle = bottomSheetView.findViewById<TextView>(R.id.tv_cancle)
                    tvReport.setOnClickListener {
                        //举报
                        result.invoke(true)
                        mBottomSheetReportDialog?.dismiss()
                    }

                    tvCancle.setOnClickListener {
                        mBottomSheetReportDialog?.dismiss()
                    }
                }
                //显示
                if (mBottomSheetReportDialog?.isShowing == false) {
                    mBottomSheetReportDialog?.show()
                } else {
                    mBottomSheetReportDialog?.dismiss()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

2、dialog_bottom_recommend_book.xml

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

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/ui_dp_500"
        android:background="@drawable/btn_shap_26"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/ui_dp_500"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            />


        <TextView
            android:id="@+id/tv_cancle"
            android:layout_width="match_parent"
            android:layout_height="@dimen/ui_dp_57"
            android:text="cancel"
            android:textColor="@color/black_6"
            android:textSize="@dimen/ui_sp_18"
            android:background="@drawable/btn_shap_27"
            android:gravity="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginHorizontal="@dimen/ui_dp_14"
            android:layout_marginBottom="@dimen/ui_dp_26"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
上一篇下一篇

猜你喜欢

热点阅读