Tuesday, 25 May 2021

PagingDataAdapter stops loading after fragment is removed and added back

I am presenting a PagingSource returned by Room ORM on a PagingDataAdapter.

The RecyclerView is present on a Fragment -- I have two such fragments. When they are switched, they stop loading the items on next page and only placehodlers are shown on scrolling.

Please view these screen captures if it isn't clear what I mean--

Relevant pieces of code (please ask if you would like to see some other part/file) -

The Fragment:

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)
        }
    }
}

View model-

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)
}


from PagingDataAdapter stops loading after fragment is removed and added back

No comments:

Post a Comment