When using Paging3 and a LazyColumn, I lose my scroll position upon phone rotation when my LazyColum...
z
When using Paging3 and a LazyColumn, I lose my scroll position upon phone rotation when my LazyColumn starts with an`item` before displaying my actual paging content Is there a proper fix for this? See thread for more info
Copy code
LazyColumn(state = scrollState) {
   item { somesearchbar() }
   itemsIndexed(searchResult, key = { _, item -> item.postId }) { _, entry ->
    EntryCard(entry)
   }
}
This does NOT work. rotation causes scroll position loss
Copy code
LazyColumn(state = scrollState) {
   itemsIndexed(searchResult, key = { _, item -> item.postId }) { _, entry ->
    EntryCard(entry)
   }
}
This works!
giving a key to the first item does nothing unfortunately
t
It's known and no official support for now you need to hide the first item until the pages are loaded. On phone but there's an issue about that.
z
One hack I found is this
Copy code
@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 programmtically
@Tolriq guess I won't show any LazyColumn at all when there's no items. This works, thanks. Will change once there's a proper fix for this
i
783 Views