【发布时间】:2021-05-22 13:54:03
【问题描述】:
我在 PagingDataAdapter 上展示 Room ORM 返回的 PagingSource。
RecyclerView 存在于片段上——我有两个这样的片段。当它们被切换时,它们会停止加载下一页上的项目,并且在滚动时只显示占位符。
如果不清楚我的意思,请查看这些屏幕截图--
- When I scroll without switching fragments, all the items are loaded
- When I switch Fragments before scrolling all the way down, the adapter stops loading new items
相关代码(请询问您是否想查看其他部分/文件)-
片段:
private lateinit var recyclerView: RecyclerView
private val recyclerAdapter = CustomersAdapter(this)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView = view.findViewById(R.id.recycler_view)
recyclerView.adapter = recyclerAdapter
recyclerView.layoutManager = LinearLayoutManager(context)
viewLifecycleOwner.lifecycleScope.launch {
viewModel.customersFlow.collectLatest { pagingData ->
recyclerAdapter.submitData(pagingData)
}
}
}
查看模型-
class CustomersListViewModel(application: Application, private val debtOnly: Boolean): ViewModel() {
private val db = AppDatabase.instance(application)
private val customersDao = db.customersDao()
val customersFlow = Pager(PagingConfig(20)) {
if (debtOnly)
customersDao.getAllDebt()
else
customersDao.getAll()
}.flow.cachedIn(viewModelScope)
}
【问题讨论】:
-
告诉我TestApp源代码,我可以帮你解决问题。
-
@DucThang 感谢您的帮助!我这里推送源代码请查看:github.com/quanta-kt/SMJ-App/tree/master
-
好的,@Quanta 让我检查一下!
-
我已经回答了你的问题,请检查是否正常
标签: android android-fragments android-recyclerview android-room android-paging