I have an application which fetches movies from the moviedb api , i'm using paging library 3 to page the data , i have set up everything and data is showing properly , the only thing that is not working in loading states , upon reading little bit more about loading state adapter , i got to know that it only works when fetching data from db after using remote mediator , i might be wrong , someone please corrects me , i would appreciate any help ..
- Code
val layoutManager = LinearLayoutManager(this)
binding.recyclerView.layoutManager = layoutManager
binding.recyclerView.setHasFixedSize(true)
movieAdapter = TrendingMovieAdapter(object : MovieListener{
override fun onMovieSelected(movieId: Int) {
Intent(this@MainActivity,DetailsActivity::class.java).apply {
putExtra("id",movieId)
startActivity(this)
}
}
})
binding.recyclerView.adapter = movieAdapter.withLoadStateHeaderAndFooter(
footer = LoaderAdapter(),
header = LoaderAdapter()
)
lifecycleScope.launch {
movieViewModel.getPagedTrendingMovies().collectLatest {
movieAdapter.submitData(it)
}
}
- LoadStatesAdapter class
class LoaderAdapter : LoadStateAdapter<LoaderAdapter.ViewHolder>() {
inner class ViewHolder(var binding : LoaderItemBinding) : RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): ViewHolder {
return ViewHolder(
DataBindingUtil.inflate(LayoutInflater.from(parent.context),
R.layout.loader_item,parent,false)
)
}
override fun onBindViewHolder(holder: ViewHolder, loadState: LoadState) {
holder.binding.progressBar.isVisible = loadState is LoadState.Loading
}
}
CodePudding user response:
I also config footer load state adapter same your code, there's doesn't anything to be wrong. Hmm, you can try call executePendingBindings()
after call holder.binding.progressBar.isVisible = loadState is LoadState.Loading
.
This is onBindViewHolder()
func:
override fun onBindViewHolder(holder: ViewHolder, loadState: LoadState) { holder.binding.progressBar.isVisible = loadState is LoadState.Loading executePendingBindings() }