Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine detectTapGestures and detectDragGesturesAfterLongPress?

We need to be able to detect both taps and drag gestures after long press on the same component in Jetpack Compose.

To detect taps we can do:

Modifier.pointerInput(graphData) {
    detectTapGestures(
        ...
    )
}

And to detect drag gestures after long press:

Modifier.pointerInput(graphData) {
    detectDragGesturesAfterLongPress(
        ...
    )
}

But it is not possible to use both at the same time, since the first detect* will consume the pointer events:

Modifier.pointerInput(graphData) {
    detectTapGestures(
        ...
    )
    detectDragGesturesAfterLongPress(
        ...
    )
}

Is it possible to achieve this in a more convenient way then rolling our own custom function that replicates most of the code in detectDragGesturesAfterLongPress?

like image 229
Ulf Andersson Avatar asked Dec 29 '25 00:12

Ulf Andersson


1 Answers

By chaining two Modifier.pointerInput(Unit) functions you will be able to detect both gestures. Of course you won't be able to detect tap if drag gesture has started

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")

            }
        )
    }
like image 69
Thracian Avatar answered Dec 31 '25 12:12

Thracian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!