Jetpack Compose:pointerInput 启动 2 次

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

我一直在尝试允许通过滑动手势在多个页面之间导航,例如在 Instagram 上滑动以获取消息时。为此,我使用了pointerInput和DetectDragGestures:

.pointerInput(Unit){
                   detectDragGestures { change, dragAmount ->
                       change.consume()

                       if (dragAmount.x < 0){
                           navController.navigate(/* Route */)
                       }
                   }
            }

但是在尝试时,导航功能似乎被调用了两次,因为当我使用后退按钮时,我必须使用它两次。

当删除这部分代码时,显然没有任何反应。 导航功能对于这条路线可以正常工作,因为我在同一页面上有一个调用它的按钮(就像前面的 Instagram 消息示例一样)。 当改变调用函数所需数量的值时,只是增加了手势的长度,但仍然调用了两次函数。

有人知道这可能来自哪里吗?

android-jetpack-compose gesture
1个回答
0
投票

DetectDragGestures 有几个阶段,选择一个进行导航

.pointerInput(Unit) {
    detectDragGestures(
        onStart = { offset -> 
            // This is called when the drag starts.
        },
        onDrag = { change, dragAmount ->
            // This is called while dragging. 
            change.consumeAllChanges()
            if (dragAmount.x < -someThreshold) {  // You can define someThreshold as a specific value, e.g., -100f
                navController.navigate(/* Route */)
            }
        },
        onStop = { 
            // This is called when the drag ends.
        }
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.