Paging 3.0 list with new params in Kotlin

五迷三道 提交于 2021-01-24 08:34:18

问题


I have the following code:

   val history: Flow<PagingData<Any>> = Pager(PagingConfig(pageSize = 10)) {
    PaginationBaseDataSource(apiService)
}.flow
    .cachedIn(viewModelScope)

This currently is displaying a list of items without any additional params. This works Ok... But now I wish to query this list based in certain params that the user can change in frontend, let´s say I wish to add the parameter 3 as a query.

   val history: Flow<PagingData<Any>> = Pager(PagingConfig(pageSize = 10)) {
    PaginationBaseDataSource(apiService, 3)
}.flow
    .cachedIn(viewModelScope)

The question is... How can I set this query parameters on the fly? Let´s say the user instead of 3, used 6 and then 9. How can I achieve this?

Thank you very much


回答1:


You'll want to emit a new PagingData anytime one of your args changes. A flow-style way of achieving this might look like:

val queryFlow = MutableStateFlow(value = 3)
val pagingDataFlow = queryFlow.flatMapLatest { query ->
    Pager(...) { MyPagingSource(query) }.flow.cachedIn(viewModelScope)
}


来源:https://stackoverflow.com/questions/64692260/paging-3-0-list-with-new-params-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!