Reorderable
Reorderable copied to clipboard
When some items in LazyVerticalGrid are not wrapped with ReorderableItem, the drag-and-drop behavior becomes inconsistent or buggy
https://github.com/user-attachments/assets/c60deaba-d9c6-49de-a3e8-4848b7ee9284
code:
package sh.calvin.reorderable.demo.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.DragHandle
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.CustomAccessibilityAction
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.customActions
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import sh.calvin.reorderable.ReorderableItem
import sh.calvin.reorderable.demo.ReorderHapticFeedbackType
import sh.calvin.reorderable.demo.items
import sh.calvin.reorderable.demo.rememberReorderHapticFeedback
import sh.calvin.reorderable.rememberReorderableLazyGridState
@Composable
fun SimpleReorderableLazyVerticalGridScreen() {
val haptic = rememberReorderHapticFeedback()
var list by remember { mutableStateOf(items) }
val lazyGridState = rememberLazyGridState()
val reorderableLazyGridState = rememberReorderableLazyGridState(lazyGridState) { from, to ->
list = list.toMutableList().apply {
add(to.index, removeAt(from.index))
}
haptic.performHapticFeedback(ReorderHapticFeedbackType.MOVE)
}
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 96.dp),
modifier = Modifier.fillMaxSize(),
state = lazyGridState,
contentPadding = PaddingValues(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
item(span = { GridItemSpan(maxLineSpan) }, key = "header") {
Box(Modifier.fillMaxWidth()
.height(100.dp).background(Color.Cyan))
}
itemsIndexed(list, key = { _, item -> item.id }) { index, item ->
ReorderableItem(reorderableLazyGridState, item.id) {
val interactionSource = remember { MutableInteractionSource() }
Card(
onClick = {},
modifier = Modifier
.height(96.dp)
.semantics {
customActions = listOf(
CustomAccessibilityAction(
label = "Move Before",
action = {
if (index > 0) {
list = list.toMutableList().apply {
add(index - 1, removeAt(index))
}
true
} else {
false
}
}
),
CustomAccessibilityAction(
label = "Move After",
action = {
if (index < list.size - 1) {
list = list.toMutableList().apply {
add(index + 1, removeAt(index))
}
true
} else {
false
}
}
),
)
},
interactionSource = interactionSource,
) {
Box(Modifier.fillMaxSize()) {
IconButton(
modifier = Modifier
.align(Alignment.TopEnd)
.draggableHandle(
onDragStarted = {
haptic.performHapticFeedback(ReorderHapticFeedbackType.START)
},
onDragStopped = {
haptic.performHapticFeedback(ReorderHapticFeedbackType.END)
},
interactionSource = interactionSource,
)
.clearAndSetSemantics { },
onClick = {},
) {
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder")
}
Text(
item.text,
Modifier.align(Alignment.Center).padding(horizontal = 8.dp),
textAlign = TextAlign.Center,
)
}
}
}
}
}
}