Zun
11/07/2022, 5:26 PMZun
11/07/2022, 5:27 PMLazyColumn(state = scrollState) {
item { somesearchbar() }
itemsIndexed(searchResult, key = { _, item -> item.postId }) { _, entry ->
EntryCard(entry)
}
}
This does NOT work. rotation causes scroll position lossZun
11/07/2022, 5:27 PMLazyColumn(state = scrollState) {
itemsIndexed(searchResult, key = { _, item -> item.postId }) { _, entry ->
EntryCard(entry)
}
}
This works!Zun
11/07/2022, 5:28 PMTolriq
11/07/2022, 5:30 PMZun
11/07/2022, 5:31 PM@Composable
fun <T : Any> LazyPagingItems<T>.rememberLazyListState2(): LazyListState {
// After recreation, LazyPagingItems first return 0 items, then the cached items.
// This behavior/issue is resetting the LazyListState scroll position.
// Below is a workaround. More info: <https://issuetracker.google.com/issues/177245496>.
return when (itemCount) {
// Return a different LazyListState instance.
0 -> remember(this) { LazyListState(0, 0) }
// Return rememberLazyListState (normal case).
else -> androidx.compose.foundation.lazy.rememberLazyListState()
}
}
which comes with the downside of not being able to scroll up programmticallyZun
11/07/2022, 5:34 PMIan Lake
11/07/2022, 5:44 PM