首页 > 解决方案 > 删除片段并重新添加后,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)
}

标签: androidandroid-fragmentsandroid-recyclerviewandroid-roomandroid-paging

解决方案


经过你的代码,我发现了问题FragmentTransaction.replace函数,flow.cachedIn(viewModelScope) 当activity调用replacefragment函数时,CustomerFragment会被销毁,它的ViewModel也会被销毁(viewModel.onCleared()触发)所以这次cachedIn(viewModelScope)也是无效的。

我有3个解决方案给你

解决方案 1:删除.cachedIn(viewModelScope)

请注意,这只是一个临时解决方案,不推荐使用。因此,Activity 上仍然存在 Fragment 实例,但 Fragment 已被破坏(内存仍在泄漏)。

解决方案 2:不要使用FragmentTransaction.replaceMain 活动中的函数,而是使用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,结果如下:

结果


推荐阅读