android - 删除片段并重新添加后,PagingDataAdapter 停止加载
问题描述
我PagingSource
在 PagingDataAdapter 上展示 Room ORM 返回的内容。
RecyclerView
存在于片段上——我有两个这样的片段。当它们被切换时,它们会停止加载下一页上的项目,并且在滚动时只显示占位符。
如果不清楚我的意思,请查看这些屏幕截图-
相关代码(请询问您是否想查看其他部分/文件) -
片段:
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)
}
解决方案
经过你的代码,我发现了问题FragmentTransaction.replace
函数,flow.cachedIn(viewModelScope)
当activity调用replace
fragment函数时,CustomerFragment会被销毁,它的ViewModel也会被销毁(viewModel.onCleared()
触发)所以这次cachedIn(viewModelScope)
也是无效的。
我有3个解决方案给你
解决方案 1:删除.cachedIn(viewModelScope)
请注意,这只是一个临时解决方案,不推荐使用。因此,Activity 上仍然存在 Fragment 实例,但 Fragment 已被破坏(内存仍在泄漏)。
解决方案 2:不要使用FragmentTransaction.replace
Main 活动中的函数,而是使用FragmentTransaction.add
函数:
它不会泄漏内存并且仍然可以使用该cachedIn
功能。应该在activity的fragment很少且fragment的视图不太复杂的情况下使用。
private fun switchNavigationFragment(navId: Int) {
when (navId) {
R.id.nav_customers -> {
switchFragment(allCustomersFragment, "Customer")
}
R.id.nav_debt -> {
switchFragment(debtCustomersFragment, "DebtCustomer")
}
}
}
private fun switchFragment(fragment: Fragment, tag: String) {
val existingFragment = supportFragmentManager.findFragmentByTag(tag)
supportFragmentManager.commit {
supportFragmentManager.fragments.forEach {
if (it.isVisible && it != fragment) {
hide(it)
}
}
if (existingFragment != fragment) {
add(R.id.fragment_container, fragment, tag)
.disallowAddToBackStack()
} else {
show(fragment)
}
}
}
解决方案 3:使用导航组件 Jetpack
这是最安全的解决方案。它可以使用 Android Studio 的模板或以下一些文章来创建。
我尝试了解决方案 1 和 2,结果如下:
推荐阅读
- php - 递归项状态性能
- java - 如何通过读取java中的路径来更新json的值?
- deep-learning - 使用图像中的多个对象训练 Faster R-CNN
- php - 如何通过读取单元格中的文本来使用 phpExcel 更改特定文本的颜色?
- adaptive-cards - 具有 Luis 意图的自适应卡片
- ios - 具有自定义非对称可点击区域的 UIButton?
- django - django rest框架的mongoengine连接connectionerror
- javascript - 当角度 6 出现任何错误时如何显示错误分量?
- r - 如果序列 <= 3 保留某些信息,则将所有值解码为零
- scrapy - 初学者帮助 - 抓取电子商务网站