You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose LazyColumn垂直拖拽失效问题修复求助

Jetpack Compose LazyColumn垂直拖拽需先水平拖拽才能生效的问题

我在Jetpack Compose的LazyColumn中通过detectDragGestures实现垂直拖拽排序功能,但遇到一个问题:直接垂直拖拽时毫无反应,必须先做一次小幅度水平拖拽后,垂直拖拽才能正常工作。

问题演示动图:
问题演示动图

实现代码如下:

val users = state.data.toMutableStateList()
val dragAndDropListState = rememberDragAndDropListState(lazyListState) { from, to ->
    users.move(from, to)
}
LazyColumn(
    modifier = Modifier
        .fillMaxSize()
        .weight(1f)
        .pointerInput(Unit) {
            val initialTouchPosition = mutableStateOf(Offset.Zero)

            detectDragGestures(
                onDrag = { change, offset ->
                    change.consume()
                    dragAndDropListState.onDrag(offset)

                    if (overscrollJob?.isActive == true) return@detectDragGestures

                    dragAndDropListState
                        .checkOverscroll()
                        .takeIf { it != 0f }
                        ?.let {
                            overscrollJob = coroutineScope.launch {
                                dragAndDropListState.lazyListState.scrollBy(it)
                            }
                        } ?: kotlin.run { overscrollJob?.cancel() }

                },
                onDragStart = { offset ->
                    if (offset.getDistance() > 10.dp.toPx()) {
                        initialTouchPosition.value = offset
                        dragAndDropListState.onDragStart(offset)
                    }
                },
                onDragEnd = { dragAndDropListState.onDragInterrupted() },
                onDragCancel = { dragAndDropListState.onDragInterrupted() },
            )
        }, state = dragAndDropListState.lazyListState
) {
    itemsIndexed(users) { index, user ->
        ItemCard(userEntityUi = user, modifier = Modifier.composed {
            val offsetOrNull = dragAndDropListState.elementDisplacement.takeIf {
                index == dragAndDropListState.currentIndexOfDraggedItem
            }
            Modifier.graphicsLayer {
                translationY = offsetOrNull ?: 0f
            }
        })
    }
}

恳请解决该问题,让垂直拖拽无需前置水平操作即可立即生效。

内容的提问来源于stack exchange,提问作者Hafiz Waqas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 15:48:24