Android Compose ModalBottomSheetLayout 外部点击

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

我想在用户点击

ModalBottomSheetLayout
之外时捕捉事件。 我用
ModalBottomSheetLayout
包裹了
Box
,它是屏幕的全尺寸并设置了点击监听器。但是 Click 侦听器仅在我单击
ModalBottomSheetLayout
而不是外部时才起作用。我想
ModalBottomSheetLayout
已经消耗了点击事件并且不允许我捕捉它?

Box(
    modifier = Modifier
        .clickable {
            Log.e("TAG", "clicked!")
        }.pointerInput(Unit) {
            detectTapGestures {
                Log.e("TAG", "tapped!")
            }
        }
        .fillMaxSize()
        .background(color = TinderTheme.colors.gray60)
) {
    ModalBottomSheetLayout(
        sheetState = sheetState,
        sheetContent = {
            content()
        }
    )
}

有没有办法在

click
之外赶上
tap
/
ModalBottomSheetLayout
的事件?

android android-jetpack-compose android-jetpack-compose-modifier android-modalbottomsheetlayout
1个回答
0
投票

ModalBottomSheetLayout api 不允许您捕获此事件,但您可以执行以下操作:

Box(
    modifier = Modifier
        .pointerInteropFilter {
            val bottomSheetShown = sheetState.currentValue != ModalBottomSheetValue.Hidden
            val outside = it.y < sheetState.offset.value

            if (bottomSheetShown && outside) {
                // do you own logic here
                Log.d("BottomSheet", "MotionEvent outside")
                true
            } else {
                false
            }
        }
) {
    ModalBottomSheetLayout(
        sheetContent = { BottomSheetContent() },
        content = { Screen(onShow = { coroutineScope.launch { sheetState.show() } }) },
        sheetState = sheetState,
    )
}

这样您将获得

if (bottomSheetShown && outside
块内的所有MotionEvents。在那里你可以处理这个事件,做你的生意,然后以太将它转发到底部表(通过返回
false
)或阻止它(通过返回
true
- 然后你的底部表将不会得到这个事件)

© www.soinside.com 2019 - 2024. All rights reserved.