Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Compose LazyColumn key, messes up scrolling when sorting the items

Posted on November 11, 2022 By

Solution 1 :

… only problem is that the animation is now gone, because it doesn’t have a key.

I created a sample alphabet letter sorting/filtering and yes, without an item key the animation stops working. Though I’m not sure why in your case you said having this makes the animation work, it doesn’t work on mine however.

items(items) { item ->

But anyway, I’ll try to make assumptions, so please bare with me.

If you think of it, without animation, any changes made on the data structure is immediately rendered to the UI, it will just simply display the new data structure, but if you add some delay/duration(animation) to those rendering changes, say sorting (e.g. switching the first and last item position):

  • Fade out last positioned item and Fade it in to first position
  • Fade out first positioned item and Fade it in to last position

the framework has to have something that can uniquely identify those position(e.g. LazyColumn's item key) so it won’t mess up while doing the animation rendering, also maybe to avoid performing some overhead just to keep track of the changes.

And as per the API documentation, it needs a key for it to animate

enter image description here

The wanted result is that you stay on top of the items, because we didn’t move.

Not sure if I understand this correctly, but if you want to stay on the first item, you can utilize a LaunchedEffect and use the list’s size as its key to execute scrolling operations to the LazyColumn every filtering.

LaunchedEffect(key1 = list.size) {
    lazyListState.animateScrollToItem(0) // scrolls to first item
}

Solution 2 :

Semi-Fixed the problem with:

LaunchedEffect(items.first()) {
    listState.animateScrollToItem(0)
}
LazyColumn{
        items(items, key = { it.id}) { item-> ...
}

Sometimes the animation skips, but it works 90% of the time. If I remove the animateScrollToItem, the animation looks better, but then there is a scroll position issue.

READ  [FIXED] javascript - How to download pdf in android through react native using web view package?
Powered by Inline Related Posts

Edit: Because I had search I had to improve the logic:

LaunchedEffect(items.firstOrNull()) {
    if (items.isNotEmpty() && searchQuery.isEmpty()) {
        listState.animateScrollToItem(0)
    }
}

This now works like it should in the beginning.

Problem :

I’m trying to implement simple sort with items that are in a state.

This will work perfectly, only problem is that the animation is now gone, because it doesn’t have a key.

LazyColumn(
    state = listState,
) {
    items(items.size) { index ->
        val ticker = items[index]
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .animateItemPlacement(), // This won't work
        ) {
            Item(...)
        }
    }
}

But If I try to change it to:

items(items.size, key = { items[it].name }) { index ->

or If I use:

items(items, key = { items[it].name }) { item ->

It will have animations, but it will move items above and below current scroll position (even if you don’t move).
The wanted result is that you stay on top of the items, because we didn’t move.

This is in a viewModel:

private val _state = MutableStateFlow<Response<List<Item>>>(Response.Loading)
val state get() = _state
private fun updateList() {
    viewModelScope.launch(Dispatchers.IO) {
        client.getItems()
            .onSuccess {
                unFilteredList = it.toMutableList()

                val filteredList = filterList()
                _tickerListState.value = Response.Success(filteredList)
            }
            .onFailure {}
    }
}

Comments

Comment posted by WinterChilly

Thank you for the answer. Sorry for the misunderstanding, You are correct, I added key to items(items) to make an animation. Now for the issue at hand. We don’t solve it with LaunchedEffect(list.size), because if you are sorting the list, size can be the same and it won’t have any effect. I’m wondering why does the scroll issue happen, because it shouldn’t scroll anywhere when you are animating items with .animateItemPlacement. To sum: If you don’t use the keys it doesn’t mess up the scroll position, but if you use items with keys, it will mess up the scroll position.

READ  [FIXED] android - Another question about icons and push notifications
Powered by Inline Related Posts

Comment posted by z.y

I can only make an honest guess but “scrolling to a position” + “items with keys” shouldnt have anything against each other, i have many implementations where both exists and I have no problem with the two. There must be something in your code that causes the “scroll issue”

Comment posted by WinterChilly

Yes of course. If the first item is the same after sorting, then there is no problem. But if the first item changes, the scroll is focused on that item and not on new first item. I’ll try with LaunchedEffect(list first item changes){ scrollToPosition}. This Works!

Android Tags:android, android-jetpack-compose, android-jetpack-compose-list, kotlin, kotlin-stateflow

Post navigation

Previous Post: Do you have to pay for MyNetDiary?
Next Post: [FIXED] Simple Cucumber Test is not running on Sample Android Project

Related Posts

[FIXED] android – How to get info about .apk file using terminal (shell)? Android
[FIXED] kotlin – How to Correctly Save the State of a Custom View in Android Android
[FIXED] Azure B2C Android login AppAuth demo – page refreshes instead of logging in and doesn’t redirect to the app Android
[FIXED] kotlin – Android Room, how to save an entity with one of the variables being a sealed class object Android
[FIXED] android – Sending Firebase Cloud Message to closed App does not work Android
[FIXED] Text size increment not working in Android Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme