结合 detectorTapGestures 和 detectorDragGesturesAfterLongPress?

Ulf*_*son 11 android kotlin android-jetpack-compose android-jetpack-compose-gesture

我们需要能够在长按 Jetpack Compose 中的同一组件后检测到点击和拖动手势。

要检测点击,我们可以这样做:

Modifier.pointerInput(graphData) {
    detectTapGestures(
        ...
    )
}
Run Code Online (Sandbox Code Playgroud)

并检测长按后的拖动手势:

Modifier.pointerInput(graphData) {
    detectDragGesturesAfterLongPress(
        ...
    )
}
Run Code Online (Sandbox Code Playgroud)

但不可能同时使用两者,因为第一个detect*将消耗指针事件:

Modifier.pointerInput(graphData) {
    detectTapGestures(
        ...
    )
    detectDragGesturesAfterLongPress(
        ...
    )
}
Run Code Online (Sandbox Code Playgroud)

是否有可能以更方便的方式实现这一点,然后滚动我们自己的自定义函数来复制 detectorDragGesturesAfterLongPress 中的大部分代码?

Thr*_*ian 17

通过链接两个 Modifier.pointerInput(Unit) 函数,您将能够检测这两个手势。当然,如果拖动手势已开始,您将无法检测到点击

val context = LocalContext.current

val modifier = Modifier
    .pointerInput(Unit) {
        detectTapGestures(
            onPress = {
                Toast
                    .makeText(context, "onPress", Toast.LENGTH_SHORT)
                    .show()
            },
            onTap = {
                Toast
                    .makeText(context, "onTap", Toast.LENGTH_SHORT)
                    .show()
            }
        )
    }

    .pointerInput(Unit) {
        detectDragGesturesAfterLongPress(
            onDragStart = {
                Toast
                    .makeText(context, "onDragStart", Toast.LENGTH_SHORT)
                    .show()
            },
            onDrag = { change, dragAmount ->
                println("DRAGGING$ dragAmount")

            }
        )
    }
Run Code Online (Sandbox Code Playgroud)