Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Save and retain LazyColumn scroll position while using Paging 3

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

over 4 years ago · Hanz Gallego
3 Respostas
Responde à pergunta

0

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: https://issuetracker.google.com/issues/177245496

over 4 years ago · Hanz Gallego Relatório

0

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.

over 4 years ago · Hanz Gallego Relatório

0

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()
    ...
}
over 4 years ago · Hanz Gallego Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda