1

I have implemente paging library 3 and it works and shows data , what i need to implement is loading more feature , so when the user scrolls down , it fetch a page 10 items , so basically between loading of new data i want to show a progressbar at the bottom but since it is my first using paging library 3 , i have no clue on how to do that , if someone could i'd appreciate it , thank you

  • I tried to show and hide progress bar based on the loadstate of my adapter class but no luck

    lifecycleScope.launch {
           newsAdapter.loadStateFlow.collectLatest { loadingState ->
               if(loadingState.refresh is LoadState.Loading){
                   binding.progressBar.visibility = View.GONE
               }
               if(loadingState.refresh is LoadState.Loading) {
                   binding.progressBar.visibility = View.VISIBLE
               }
               if(loadingState.refresh is LoadState.NotLoading){
                   binding.progressBar.visibility = View.GONE
               }

           }
       }
Taki
  • 3,290
  • 1
  • 16
  • 41

2 Answers2

2

Looking at https://medium.com/@yash786agg/jetpack-paging-3-0-android-bae37a56b92d, I found that we should also observe append state, as @dlam wrote.

adapter.addLoadStateListener { loadState ->

    if (loadState.refresh is LoadState.Loading ||
        loadState.append is LoadState.Loading)
        // Show ProgressBar
    else {
        // Hide ProgressBar

        // If we have an error, show a toast
        val errorState = when {
            loadState.append is LoadState.Error -> loadState.append as LoadState.Error
            loadState.prepend is LoadState.Error ->  loadState.prepend as LoadState.Error
            loadState.refresh is LoadState.Error -> loadState.refresh as LoadState.Error
            else -> null
        }
        errorState?.let {
            Toast.makeText(this, it.error.toString(), Toast.LENGTH_LONG).show()
        }
    }
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

LoadState for scrolling down is CombinedLoadStates.append, not .refresh which is when the list gets replaced (initial load / refresh).

Actually if you're interested in LoadState aware footer in the style of ConcatAdapter, paging has a built-in for this, see: https://developer.android.com/topic/libraries/architecture/paging/v3-paged-data#load-state-adapter

dlam
  • 3,547
  • 17
  • 20