在Kotlin中使用新参数的Paging 3.0列表 [英] Paging 3.0 list with new params in Kotlin

查看:111
本文介绍了在Kotlin中使用新参数的Paging 3.0列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

当前显示的是没有任何其他参数的项目列表.可以,但是...现在,我希望根据用户可以在前端更改的某些参数来查询此列表,比方说,我希望将参数3添加为查询.

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)

问题是...如何即时设置此查询参数?假设用户不是3,而是6,然后是9.我该如何实现?

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?

非常感谢

推荐答案

只要您的args发生任何更改,您都将要发出一个新的PagingData.实现该目标的流式方式可能类似于:

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)
}

这篇关于在Kotlin中使用新参数的Paging 3.0列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆