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
}
})
}
}
I’m implementing detectDragGestures in a Jetpack Compose LazyColumn to allow vertical dragging. However, I’ve noticed an issue where dragging vertically doesn’t work unless I first make a small horizontal drag gesture. After that, vertical dragging starts working as expected.
How can I fix this issue to ensure vertical dragging works immediately without requiring any horizontal movement?