Skip to content

Commit

Permalink
Improve Drag and Drop in LazyColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
jeluchu committed Jul 6, 2023
1 parent b58fdce commit e9103f6
Showing 1 changed file with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ class DragDropState internal constructor(
draggingItem.index != item.index
}
if (targetItem != null) {
val scrollToIndex = when {
targetItem.index == state.firstVisibleItemIndex -> draggingItem.index
draggingItem.index == state.firstVisibleItemIndex -> targetItem.index
else -> null
val scrollToIndex = if (targetItem.index == state.firstVisibleItemIndex) {
draggingItem.index
} else if (draggingItem.index == state.firstVisibleItemIndex) {
targetItem.index
} else {
null
}
if (scrollToIndex != null) {
scope.launch {
Expand Down Expand Up @@ -172,7 +174,7 @@ fun Modifier.dragContainer(dragDropState: DragDropState): Modifier {
}
}

@ExperimentalFoundationApi
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyItemScope.DraggableItem(
dragDropState: DragDropState,
Expand All @@ -181,22 +183,19 @@ fun LazyItemScope.DraggableItem(
content: @Composable ColumnScope.(isDragging: Boolean) -> Unit
) {
val dragging = index == dragDropState.draggingItemIndex
val draggingModifier = when {
dragging -> {
Modifier
.zIndex(1f)
.graphicsLayer {
translationY = dragDropState.draggingItemOffset
}
}
index == dragDropState.previousIndexOfDraggedItem -> {
Modifier
.zIndex(1f)
.graphicsLayer {
translationY = dragDropState.previousItemOffset.value
}
}
else -> Modifier.animateItemPlacement()
val draggingModifier = if (dragging) {
Modifier
.zIndex(1f)
.graphicsLayer {
translationY = dragDropState.draggingItemOffset
}
} else if (index == dragDropState.previousIndexOfDraggedItem) {
Modifier.zIndex(1f)
.graphicsLayer {
translationY = dragDropState.previousItemOffset.value
}
} else {
Modifier.animateItemPlacement()
}
Column(modifier = modifier.then(draggingModifier)) {
content(dragging)
Expand Down

0 comments on commit e9103f6

Please sign in to comment.