Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 259 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save and retain LazyColumn scroll position while using Paging 3

#1
I am using the Paging 3 Library with Lazy Column and a BottomNavigation Menu in my Activity. Each Composable Screen attached to the BottomNavMenu uses a Composable that in turn uses Lazy Columns. When I navigate between the Composables using the compose navigation library I want the recomposed composable to retain the scroll position and the lazyListState

I have tried the following but doesn't work:


val listState = rememberLazyListState()
val scrollState = rememberScrollState()

LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(bottom = 56.dp)
.scrollable(scrollState, Orientation.Vertical),
state = listState,
) {

//draw the items and hook the loadState to the lazy paging items


Every time I navigate to this composable, it is recomposed with the scroll position set to 0 which is not what I want.
What is the right way to handle this
Reply

#2
You probably need to do `collectAsLazyPagingItems()` before declaring your `NavHost`

I filed a bug about this you can read it for more detail and star it to follow the issue:

[To see links please register here]

Reply

#3
It seems like this a bug in the Compose Navigation component.

`rememberLazyListState()` does not work correctly on configuration changes (screen rotation, dark theme toggle etc) for composables inside a `NavHost`.
Reply

#4
You can cache paging data in a ViewModel and then collect it in the UI. The ViewModel part can look something like this:

val items: Flow<PagingData<YourItem>> =
Pager(PagingConfig(PAGE_SIZE)) {
YourSource()
}
.flow
.cachedIn(viewModelScope)
And then you can use it in the UI like this:

@Composable
fun YourScreen(viewModel: YourViewModel) {
val items = viewModel.items.collectAsLazyPagingItems()
...
}
Reply

#5
To solve this problem I used:

val items: Flow<PagingData<YourItem>> =
Pager(PagingConfig(PAGE_SIZE)) {
YourSource()
}
.flow
.cachedIn(GlobalScope)

With the respective clean in the viewmodel
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through