带有分页 3 的多页 viewPager2 不使用 SharedViewModel 在第 2 页上显示数据
Multi page viewPager2 with paging 3 doesn't show data on page 2 using SharedViewModel
我想实现两个视图选项卡,回收视图作为列表,使用分页 3 收集数据,return 将是 kotlin 流程。在选项卡一中一切正常,但在选项卡二中没有任何显示。
viewModel代码(SharedViewModel):
@ExperimentalPagingApi
@HiltViewModel
class MovieViewModel @Inject constructor(
private val repository: MovieRepository,
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private val TAG = "MovieVM"
private var _moviePaging: Flow<PagingData<MovieEntities>>? =
repository.getMovie().cachedIn(viewModelScope)
private var _tvPaging: Flow<PagingData<MovieEntities>>? =
repository.getTv().cachedIn(viewModelScope)
init {
loadMovie()
}
// new paging
var moviePaging = MutableStateFlow<PagingData<MovieEntities>>(PagingData.empty())
var tvPaging = MutableStateFlow<PagingData<MovieEntities>>(PagingData.empty())
private fun loadMovie() {
viewModelScope.launch(dispatcher) {
_moviePaging?.collectLatest {
moviePaging.value = it
}
_tvPaging?.collectLatest {
tvPaging.value = it
}
}
}
密码是运行。当我调试时,只有 _moviePaging
调用并显示记录器改造 GET,但是 _tvPaging
什么都没有,而不是调用 api,就像从未触发到 运行(无法访问?)
所以,我正在更改订单,请先致电 _tvPaging
。那么只有 _tvPaging
是 运行.
我想要两行代码 _moviePaging
和 _tvPaging
运行ning,但现在只是其中之一。请帮助。
如有回应将不胜感激。
在阅读了一些文档并了解了工作流协程之后,
one coroutine scope for hot flow only one methode can run, they suspend forever
所以,解决方案是viewModelScope
每个热流
viewModelScope.launch(dispatcher) {
_moviePaging?.collectLatest {
moviePaging.value = it
}
}
viewModelScope.launch(dispatcher) {
_tvPaging?.collectLatest {
tvPaging.value = it
}
}
我想实现两个视图选项卡,回收视图作为列表,使用分页 3 收集数据,return 将是 kotlin 流程。在选项卡一中一切正常,但在选项卡二中没有任何显示。
viewModel代码(SharedViewModel):
@ExperimentalPagingApi
@HiltViewModel
class MovieViewModel @Inject constructor(
private val repository: MovieRepository,
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private val TAG = "MovieVM"
private var _moviePaging: Flow<PagingData<MovieEntities>>? =
repository.getMovie().cachedIn(viewModelScope)
private var _tvPaging: Flow<PagingData<MovieEntities>>? =
repository.getTv().cachedIn(viewModelScope)
init {
loadMovie()
}
// new paging
var moviePaging = MutableStateFlow<PagingData<MovieEntities>>(PagingData.empty())
var tvPaging = MutableStateFlow<PagingData<MovieEntities>>(PagingData.empty())
private fun loadMovie() {
viewModelScope.launch(dispatcher) {
_moviePaging?.collectLatest {
moviePaging.value = it
}
_tvPaging?.collectLatest {
tvPaging.value = it
}
}
}
密码是运行。当我调试时,只有 _moviePaging
调用并显示记录器改造 GET,但是 _tvPaging
什么都没有,而不是调用 api,就像从未触发到 运行(无法访问?)
所以,我正在更改订单,请先致电 _tvPaging
。那么只有 _tvPaging
是 运行.
我想要两行代码 _moviePaging
和 _tvPaging
运行ning,但现在只是其中之一。请帮助。
如有回应将不胜感激。
在阅读了一些文档并了解了工作流协程之后,
one coroutine scope for hot flow only one methode can run, they suspend forever
所以,解决方案是viewModelScope
每个热流
viewModelScope.launch(dispatcher) {
_moviePaging?.collectLatest {
moviePaging.value = it
}
}
viewModelScope.launch(dispatcher) {
_tvPaging?.collectLatest {
tvPaging.value = it
}
}