我在我的活动中使用了Paging3库和Lazy列和BottomNavigation菜单。附加到BottomNavMenu的每个可合成屏幕都使用一个可合成屏幕,该可合成屏幕又使用惰性列。当我使用compose导航库在composable之间导航时,我希望重新组合的composable保留滚动位置和lazyListState
我尝试了以下方法,但不起作用:
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每次我导航到这个composable时,它都会被重新组合,滚动位置设置为0,这并不是我想要的。处理这个问题的正确方法是什么?
发布于 2021-08-31 14:35:10
您可以在ViewModel中缓存分页数据,然后在UI中收集这些数据。ViewModel部件可能如下所示:
val items: Flow<PagingData<YourItem>> =
    Pager(PagingConfig(PAGE_SIZE)) {
        YourSource()
    }
    .flow
    .cachedIn(viewModelScope)然后您可以在UI中使用它,如下所示:
@Composable
fun YourScreen(viewModel: YourViewModel) {
    val items = viewModel.items.collectAsLazyPagingItems()
    ...
}https://stackoverflow.com/questions/66744977
复制相似问题