ComposeReorderable icon indicating copy to clipboard operation
ComposeReorderable copied to clipboard

RememberReorderableLazyListState does not update when using a dynamic list

Open jzamith-bliss opened this issue 1 year ago • 2 comments

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?

jzamith-bliss avatar Jun 22 '23 17:06 jzamith-bliss

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.

ipdxl avatar Jul 07 '23 18:07 ipdxl

@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).

rylexr avatar Jul 15 '23 18:07 rylexr