简体   繁体   English

Android Paging 3 jetpack 撰写如何在导航返回时滚动到顶部或重置分页

[英]Android Paging 3 jetpack compose how to scroll to top or reset the paging when navigating back

I have been stuck on this for a while now.我已经坚持了一段时间了。 I have implemented Paging 3 with jetpack compose along with a remote mediator (Room Database) and its working fine.我已经使用 jetpack compose 以及远程调解器(房间数据库)实现了 Paging 3,并且工作正常。 I just want to reset the paging and scroll to the top of the list when I navigate back to the screen of the paged list.当我导航回分页列表的屏幕时,我只想重置分页并滚动到列表顶部。 animateToScroll(0) does not work it just scrolls the list on top of the current page. animateToScroll(0) 不起作用,它只是滚动当前页面顶部的列表。 What I want is reset the entire list and start again like when its first opened.我想要的是重置整个列表并重新开始,就像它第一次打开时一样。

I now have the same need and I solved it like this.我现在有同样的需求,我就这样解决了。

@HiltViewModel
class IndexViewModel @Inject constructor() : ViewModel() {

    ...

    var pageFlow by mutableStateOf<Flow<PagingData<Item>>>(flowOf())
        private set

    init {
        combine(section, sort, retry, ...) 
        { ... }
        .mapLatest {params->

            coroutineScope{
                val pager = Pager(...) {...}
            
                pageFlow = pager.flow.cachedIn(this)
            }

        }.launchIn(viewModelScope)
    }

or the same with viewModelScope.launch and .collectLatest或与viewModelScope.launch.collectLatest相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM