Android Compose AnchoredDraggable - 监听已完成的拖动

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

我正在使用 Compose 的

AnchoredDraggable
垂直拖动一些项目,并且我想在每次到达结束锚点时调用回调(特别是结束锚点 - 我不想监听项目何时返回到开始锚定)。有什么办法可以让我听到这个吗?我尝试使用
MutableInteractionSource
,然后调用
collectIsDraggedAsState()
来了解何时调用我的回调,但它会在拖动开始时立即触发,而不是在拖动结束时触发。

我有:

val state = remember {
    AnchoredDraggableState(
        initialValue = DragAnchor.START,
        positionalThreshold = { distance: Float -> distance * 0.5f },
        velocityThreshold = { with(density) { 100.dp.toPx() } },
        animationSpec = tween(),
    )
}
val interactionSource = remember { MutableInteractionSource() }
val isDragged = interactionSource.collectIsDraggedAsState()

if (isDragged.value) {
    onDragComplete() // callback I want to invoke
}

MyComposable(
    // omitted the other modifiers
    modifier = Modifier
        .anchoredDraggable(
            state = state,
            orientation = Orientation.Vertical,
            reverseDirection = false,
            interactionSource = interactionSource
        )
)
android android-jetpack-compose android-jetpack-compose-gesture
1个回答
0
投票

您可以在

AnchoredDebuggableState
函数中结合使用
LaunchedEffect
snapshotFlow
来捕获对
@Composable
的更改。

val anchoredDraggableState = remember() {
    AnchoredDraggableState(...))
}

LaunchedEffect(anchoredDraggableState) {
    snapshotFlow { anchoredDraggableState }.collect { state ->
        // you can handle the state change here
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.