How to handle touch event interaction communication between parent/child LayoutNode. I have a Box wi...
p

Pablichjenkov

over 2 years ago
How to handle touch event interaction communication between parent/child LayoutNode. I have a Box within a HorizontalPager, the Box consume the touch event stream, something like:
var pointerEnabled by remember(someState) { mutableStateOf(true) }

Box(
            modifier = Modifier
                .fillMaxSize()
                .pointerInputModifierSwitcher(pointerEnabled, topBarState) {
                    detectDragGestures(
                        onDragStart = { offset ->
                            if (offset.x > 50) {
                                pointerEnabled = false
                            }
                            startX = offset.x
                        },
                        ...
                 }
)

// where pointerInput is added/removed from the modifiers depending on `pointerEnabled`
fun Modifier.pointerInputModifierSwitcher(
    enable: Boolean,
    key1: Any?,
    block: suspend PointerInputScope.() -> Unit
): Modifier {
    return if (enable) {
        this.pointerInput(key1, block)
    } else {
        this
    }
}
Now, If the drag gesture starts with offset.x > 50, I don’t want the Box to consume it but the HorizontalPager to do so. I set pointerEnabled to false so it triggers recomposition and the pointerInput is removed in the next recomposition. The problem is that once the drag gesture starts with offset.x > 50, when I cancel/remove the pointerInput modifier from the Box, the events are not propagated to the Pager until the drag gesture finishes. It is until the next drag gesture that the Pager gets to handle the touch events. What is the way to switch/divert the touch events to the parent (in this case the Pager), in the same gesture that is taking place. In the classic system there used to be an interceptTouch in the ViewGroup class that you could override, or also returning false from the onTouchEvent() would let the parent know you are not interested in handling the touch stream. Any similar in compose?