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
相关产品推荐
相关产品推荐

