设置BottomSheetDialogFragment不可拖拽

第一种、将dialog的高度和behavior的setPeekHeight的高度设置为一致的时候,就可以让该控件不可拖拽

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog

        bind = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_car_often_group, null, false)

        val wm = activity!!.windowManager
        val height = wm.defaultDisplay.height
        mMaxSlideHeight = (0.85f * height).toInt()
        dialog.setOnShowListener {
            val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
            val behavior = BottomSheetBehavior.from(bottomSheet!!)
            behavior.state = BottomSheetBehavior.STATE_EXPANDED
            //设置peek高度
    	behavior.setPeekHeight(mMaxSlideHeight)
     	//是否下拉关闭dialog
      	//behavior.setHideable(false)
        }

    	
        if (mMaxSlideHeight > 0) {
            //设置dialog高度
    	val params =
                ViewGroup.LayoutParams(
                	ViewGroup.LayoutParams.MATCH_PARENT, 
                	mMaxSlideHeight)
            dialog.setContentView(bind.root, params)
        } else {
            val params = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            dialog.setContentView(bind.root, params)
        }

        // Do something with your dialog like setContentView() or whatever
        return dialog
    }

第二种、设置addBottomSheetCallback监听,在onStateChanged的状态变化时都设置为展开状态,同时要设置最大高度。但是这样就会导致下拉关闭功能失效

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog

    bind = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_car_often_group, null, false)

    val wm = activity!!.windowManager
    val height = wm.defaultDisplay.height
    val mMaxSlideHeight = (0.55f * height).toInt()
    dialog.setOnShowListener {
        val bottomSheet = (it as BottomSheetDialog).findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
        val behavior = BottomSheetBehavior.from(bottomSheet!!)
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                    behavior.state = BottomSheetBehavior.STATE_EXPANDED
                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {}
        })
        behavior.setHideable(false)
    }


    if (mMaxSlideHeight > 0) {
        val params =
            ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mMaxSlideHeight)
        dialog.setContentView(bind.root, params)
    } else {
        val params = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        dialog.setContentView(bind.root, params)
    }

    // Do something with your dialog like setContentView() or whatever
    return dialog
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值