trying to figure out some weird issue I'm getting ...
# compose
t
trying to figure out some weird issue I'm getting with item animation inside a LazyColumn - it was fading in all the items at the same time every time I added a new item. When I stopped using the copy of the LazyListState from inside my AppState and started using rememberLazyListState() directly, it now properly faded in only the one item. So I refactored to a rememberAppState() method so I could call rememberLazyListState() and then stash that state into my state object - now it's broken again. Now trying to figure out why using state objects acquired at different lines of code is changing the behaviour.
I also spent a decent amount of time just trying to get rid of the reference to LazyListState from my AppState, but I am using it to request a scroll to the bottom when a new item is added 😞
Reduced example, can swap which line is commented and the behaviour changes much more than expected...
Copy code
import androidx.compose.animation.core.tween
import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.darkColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

@OptIn(ExperimentalUuidApi::class)
data class HistoryEntry(val id: Any = Uuid.random())

class AppState {
    val outputState = LazyListState()
    val history: MutableList<HistoryEntry> = mutableStateListOf()

    fun execute(scope: CoroutineScope) {
        history.add(HistoryEntry())

        scope.launch {
            outputState.animateScrollToItem(history.lastIndex)
        }
    }
}

@Composable
fun MainContent(appState: AppState, scope: CoroutineScope) {
    Column(Modifier.fillMaxSize().padding(16.dp)) {
        Box(modifier = Modifier.weight(1.0f)) {

            // Switch which of these is commented out for interesting results:
            val lazyListState = appState.outputState
//            val lazyListState = rememberLazyListState()

            LazyColumn(
                modifier = Modifier
                    .testTag("HistoryList")
                    .fillMaxSize()
                    .wrapContentHeight(Alignment.Bottom),
                state = lazyListState,
            ) {
                items(items = appState.history, key = HistoryEntry::id) { item ->
                    Box(
                        modifier = Modifier
                            .animateItem(
                                fadeInSpec = tween(durationMillis = 2000),
                                fadeOutSpec = tween(durationMillis = 2000),
                                placementSpec = tween(durationMillis = 1000),
                            )
                    ) {
                        Column {
                            val text = item.id.toString()
                            Text(text = text, maxLines = 1)
                        }
                    }
                }
            }

            VerticalScrollbar(
                adapter = rememberScrollbarAdapter(lazyListState),
                modifier = Modifier
                    .align(Alignment.CenterEnd)
                    .fillMaxHeight(),
            )
        }

        Button(onClick = { appState.execute(scope) }) {
            Text("Add Item")
        }
    }
}

fun main() = application {
    val appState = remember { AppState() }

    MaterialTheme(colorScheme = darkColorScheme()) {
        Window(onCloseRequest = ::exitApplication) {
            val scope = rememberCoroutineScope()

            MainContent(appState, scope)
        }
    }
}
Even more wildly, commenting out just
animateScrollToItem
also fixes it
of course, then when it gets to filling up the screen, it no longer scrolls to the bottommost item, so that isn't a workable workaround