Here's the original list before I add any new item
When I add a new item to the end of the list, without scrolling, the first item still remains in that position
But when I add new items to the start of the list, all the old items is moved down. Notice that New item 0 is not at the very top of the list anymore
How can I make it so that if I add a new item to the start of the list, the old item still remain in the exact position. And these new items will only be visible when the user scrolls up, not pushing everything down. If I remember correctly, it's the default behavior with RecyclerView
Here's my code:
MainActivity.kt
class MainActivity : ComponentActivity() {
private val list = MutableStateFlow<List<String>>(emptyList())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
EmptyComposeTheme {
val items = list.collectAsState()
Box(Modifier.fillMaxSize()) {
LazyColumn(
Modifier.fillMaxSize(),
) {
itemsIndexed(items.value) { index: Int, item: String ->
Text(item, color = MaterialTheme.colors.onSurface)
}
}
FloatingActionButton(onClick = {
val oldList = list.value.toMutableList()
oldList.add("New item ${oldList.size}")
list.value = oldList
}, Modifier.align(Alignment.BottomStart)) {
Text("Click me!")
}
FloatingActionButton(onClick = {
val oldList = list.value.toMutableList()
oldList.add(0, "New item reversed ${oldList.size}")
list.value = oldList
}, Modifier.align(Alignment.BottomEnd)) {
Text("Click other me!")
}
}
}
}
}
}
I'm using Compose version 1.0.1


