简体   繁体   中英

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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