因此,我有两个流具有来自两个不同 Api 的相同响应。我只有一个 recyclerview 分页适配器。现在,当满足某些条件时,如何在两个流程之间切换。例如,如果有人输入,我有一个 SearchView,我必须调用一个 API 来获取我一个响应。如果我清除 SearchView 我必须从不同的 api 返回响应,但返回到附加的同一个 Recyclerview。
val mySecondList =
flowOf(
clearListCh.receiveAsFlow().map { PagingData.empty() },
Pager (PagingConfig(pageSize = 1)) {
somePagingSource(aedApi)
}.flow.cachedIn(viewModelScope)
).flattenMerge(2)
val searchFlow = mutableSearchData.flatMapLatest {
IOLog.d(TAG, "search Flow happening..$it")
val jsonObject = JSONObject()
jsonObject.put("keyword", it)
val gson = getGsonObject(jsonObject)
Pager(PagingConfig(pageSize = 1, enablePlaceholders = true)) {
someSearchSource(aedApi, gson)
}.flow
}
private val clearListCh = Channel<Unit>(Channel.CONFLATED)
private val mutableLIve: MutableLiveData<String> = MutableLiveData()
val liveStringData = mutableLIve
fun clearListChannel() {
IOLog.d(TAG, "clearListCh called..")
clearListCh.offer(Unit)
}
在我的 onCreate 活动中,我正在初始化如下所示的内容
lifecycleScope.launchWhenStarted {
viewmodel.searchFlow.collectLatest {
IOLog.d("mutableSearchData", it.toString())
adapter?.submitData(it)
}
}
lifecycleScope.launchWhenStarted {
viewmodel.mySecondList.collectLatest {
adapter?.submitData(it)
}
}
如何在从一个范围发送到另一个范围的 submitData 之间切换