Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RememberReorderableLazyListState does not update when using a dynamic list #274

Open
jzamith-bliss opened this issue Jun 22, 2023 · 2 comments

Comments

@jzamith-bliss
Copy link

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 jzamith-bliss changed the title rememberReorderableLazyListState does not update when suign dynamic list RememberReorderableLazyListState does not update when suign dynamic list Jun 22, 2023
@jzamith-bliss jzamith-bliss changed the title RememberReorderableLazyListState does not update when suign dynamic list RememberReorderableLazyListState does not update when using a dynamic list Jun 22, 2023
@ipdxl
Copy link

ipdxl commented Jul 7, 2023

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
Copy link

rylexr commented Jul 15, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants