aclassen / ComposeReorderable

Enables reordering by drag and drop in Jetpack Compose (Desktop) LazyList & LazyGrid.
Apache License 2.0
832 stars 87 forks source link

RememberReorderableLazyListState does not update when using a dynamic list #274

Open jzamith-bliss opened 1 year ago

jzamith-bliss commented 1 year ago

The is an issue when the list is coming from the API/Database, and takes 1 screen recomposition to update the state, and therefore the list itself. By that time the sreen already recomposed and the index value is 0 due to the empty list in the first screen composition.. In this case, the items will appear on the screen but, when we try to move an item this error occours: (java.lang.IndexOutOfBoundsException: Index: 6, Size: 0 at java.util.ArrayList.remove(ArrayList.java:503). The rememberReorderableLazyListState does not provide a way to be updated when the list is updated. Is there a way to achieve or fix this issue?

ipdxl commented 1 year ago

I have the same problem, after adding or removing items from the list. the reorder doesn't work. need to close and reopen the screen to be able to drag the items.

rylexr commented 1 year ago

@jzamith-bliss @ipdxl I faced the same issue. Use rememberUpdatedState instead of remember to fix this issue as the content is dynamic. For instance:

    val data = rememberUpdatedState(dynamicItems) as MutableState
    val state = rememberReorderableLazyListState(onMove = { from, to ->
        data.value = data.value.toMutableList().apply {
            add(to.index, removeAt(from.index))
        }
    })
    ...

You can read more information about it here https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#rememberUpdatedState(kotlin.Any).