estevanfick
04/08/2025, 4:28 PMmutableStateOf
to store the list, the drag and drop works, but the button to move the list items doesn't. If I use the list directly on the LazyColumn, the button to move the items works, but the drag and drop don't. Is there a way for the state to handle both situations correctly? Code on threadestevanfick
04/08/2025, 4:28 PM@Composable
fun Screen(
list: ImmutableList<Item>,
onItemAddedOrRemoved: (item: Item) -> Unit,
) {
var stateList by remember { mutableStateOf(list) }
val listState = rememberLazyListState()
val dragDropState =
rememberDragDropState(listState) { from, to ->
stateList = stateList.toMutableList().apply {
val fromIndex = indexOfFirst { it.id == from.key }
val toIndex = indexOfFirst { it.id == to.key }
add(toIndex, removeAt(fromIndex))
}.toImmutableList()
}
LazyColumn(
state = listState,
) {
itemsIndexed(stateList, key = { _, item -> item.id }) { index, item ->
Zach Klippenstein (he/him) [MOD]
04/08/2025, 4:44 PMestevanfick
04/08/2025, 4:48 PMImage(
painter = painterResource(R.drawable.icn_minus),
contentDescription = null,
modifier = Modifier
.size(32.dp)
.clickable {
onItemAddedOrRemoved(item)
}
)
The Image to drag and drop:
Image(
painter = painterResource(R.drawable.icn_reorder),
contentDescription = null,
modifier = Modifier
.size(32.dp)
.dragContainer(dragDropState, item.id)
)
Zach Klippenstein (he/him) [MOD]
04/08/2025, 4:50 PMclickable
fires a callback out to the caller of your Screen
composable, but doesn't update stateList
at all.Zach Klippenstein (he/him) [MOD]
04/08/2025, 4:50 PMlist
, it's a no-op since stateList
is only initialized from list
on the first compositionZach Klippenstein (he/him) [MOD]
04/08/2025, 4:51 PMestevanfick
04/08/2025, 4:54 PMZach Klippenstein (he/him) [MOD]
04/08/2025, 5:01 PMonItemAddedOrRemoved
callback to support all the modification operations you need, and then just rely on the caller to correctly update the list, and get rid of stateList
. Using index to drive the operations could be tricky, since if multiple updates happen on the same frame the indices would be out of date, so i would recommend using IDs instead.estevanfick
04/08/2025, 5:14 PMSeri
04/08/2025, 5:17 PM