Android Paging 3 doesnt show Loadstate Adapter
Asked Answered
D

1

8

I have followed a tutorial to add Loadstate Adapter to the Recyclerview Adapter on Android Paging 3 but currently, it's not showing. This is how I am updating the Adapter.

 lifecycleScope.launch {
            viewModel.searchProducts(searchParam, channelId, categoryId)
                .collectLatest {
                    binding.allProductRecyclerView.isVisible = true
                    adapter.submitData(it)
                }
        

This is how I am adding the LoadState Adapter

  binding.allProductRecyclerView.adapter = adapter.withLoadStateFooter(
        footer = ProductLoadingStateAdapter()
    )

This is the gist of LoadStateAdapter alsoActivity Layout and load state Item

The adapter is working fine, I am also able to get the load states by adding LoadStateListener. It's only the Load State Adapter that isn't working. I have followed and cloned this and it's working perfectly. What might be the issue with mine?

Dihybrid answered 26/8, 2020 at 11:22 Comment(2)
Have you found any solutions for this?Field
Have you found a work around for this, I have the same issueIncorporator
A
0

Try to override the following method in your ProductLoadingStateAdapter like this:

class ProductLoadingStateAdapter: LoadStateAdapter<XXX>() {
   override fun onBindViewholder...

   override fun onCreateViewHolder...
  
   override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
      return loadState is LoadState.Loading || loadState is LoadState.Error || LoadState.NotLoading
   }
}

If you look into the source code of LoadStateAdapter, it only emits Loading and Error load states to the adapter by default, which means the adapter won't know if the loading process has finished.

So in your case, the following code will always results progress.visibility to be VISIBLE. Because you can only get LoadState.Loading state in your onBindViewHolder.

//loadState is always LoadState.Loading
if (loadState is LoadState.Loading) progress.visibility =
            View.VISIBLE; txtErrorMessage.visibility = View.GONE
Algor answered 2/12, 2020 at 2:44 Comment(2)
loadState is LoadState.Loading is not correct. loadState.mediator?.refresh is LoadState.Loading would be correct.Daumier
my footer for end of list was not showing, overriding displayLoadStateAsItem and always returning true saved the dayAnkylosis

© 2022 - 2024 — McMap. All rights reserved.