如何更改BottomSheetDialog的关闭行为的敏感性

问题描述 投票:0回答:1

我有一个BottomSheetDialogFragment。但是即使是最细微的向下滑动,也不会打开对话框。我不想使其保持静态并向下滑动以消除行为。我希望能够更改灵敏度,如果向下滑动x像素,请关闭

android android-studio kotlin
1个回答
0
投票

使用BottomSheetBehavior

这将获得您的BottomSheetDialogFragment视图的行为


var mBehavior: BottomSheetBehavior<*> = BottomSheetBehavior.from([your view reference])

然后您可以像这样设置

val dismissOffset: Float = [-1..0] // 0 is the starting position. -1 is hidden. -0.5 is middle

var offset: Float? = null

mBehavior.setBottomSheetCallback(object : BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                if (newState == BottomSheetBehavior.STATE_SETTLING) {
                    if (offset!! > dismissOffset) {
                        mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED)
                    } else {
                        mBehavior.setState(BottomSheetBehavior.STATE_HIDDEN)
                    }
                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                offset = slideOffset
            }
        })
© www.soinside.com 2019 - 2024. All rights reserved.