androidloaderandroid-paging-3

How to show loader and empty data layout in paging 3 android studio


I want to show loader when data is loading and when data loaded the loader will be dismiss. if their is no data means black or empty list then show a empty message layout screen in paging 3 android .


Solution

  • this is below code solve my problem .

    private fun observeConsultations() {
        lifecycleScope.launchWhenCreated {
    
            viewModel.getConsultationList().collectLatest{
                launch(Dispatchers.Main){
                    adapter.loadStateFlow.collectLatest { loadStates ->
                        if (loadStates.refresh is LoadState.Loading ){
                            loader.show()
                        }
                        else{
                            loader.dismiss()
                            if (loadStates.refresh is LoadState.Error){
                                if ( adapter.itemCount < 1){
                                    binding.clNoConsult.visibility = View.VISIBLE
                                }else{
                                    binding.clNoConsult.visibility = View.GONE
                                }
                            }
                        }
                    }
                }
                adapter.submitData(it)
            }
    
        }
    }