问题
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