在 BottomSheetBehavior 外部单击时拦截 BottomSheetBehavior 外部的单击处理程序

Intercepting click handler outside BottomSheetBehavior when clicked outside BottomSheetBehavior

我找到了在 外部单击时折叠 BottomSheetBehavior 的解决方案。 link 中的代码如下(转换为 Kotlin):

override fun dispatchTouchEvent(event: MotionEvent): Boolean {
    var returnValue: Boolean = super.dispatchTouchEvent(event)
    if (event.action == MotionEvent.ACTION_DOWN) {
        if (mBottomSheetBehavior?.state == BottomSheetBehavior.STATE_EXPANDED) {
            val outRect = Rect()
            val fragment = supportFragmentManager.findFragmentById(R.id.queueChoicePanel)
            fragment?.view?.getGlobalVisibleRect(outRect)

            if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
                mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
            }
        }
    }

    return returnValue
}

但是,它缺少的是当我在外部单击并单击可单击区域时,它还会触发该可单击区域的单击处理程序。我想要它,所以当 BottomSheetBehavior 处于其展开状态(即 BottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED)并且我在外部单击时,我折叠 BottomSheetBehavior 并拦截点击,以便它不会进一步触发点击处理程序从 BottomSheetBehavior 外部(例如,单击 BottomSheetBehavior 外部的按钮并禁用按钮的点击处理程序触发)。我该怎么做?

尝试在调用 super 之前进行检查,并在折叠 sheet 的情况下添加 return true,它应该消耗触摸事件,因此下面的视图不会收到它。

override fun dispatchTouchEvent(event: MotionEvent): Boolean {
    if (event.action == MotionEvent.ACTION_DOWN) {
        if (mBottomSheetBehavior?.state == BottomSheetBehavior.STATE_EXPANDED) {
            val outRect = Rect()
            val fragment = supportFragmentManager.findFragmentById(R.id.queueChoicePanel)
            fragment?.view?.getGlobalVisibleRect(outRect)

            if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
                mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
                return true
            }
        }
    }    
    return super.dispatchTouchEvent(event)
}