我有一个应用程序,可以从moviedb api中获取电影,我使用分页库3对数据进行分页,我已经设置了所有内容,数据正在正确显示,唯一在加载状态下不工作的东西,在阅读了更多关于加载状态适配器的内容后,我才知道它只在使用远程中介器从db获取数据时才能工作,我可能错了,请有人纠正我,我很感谢您的帮助。
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)
}
}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
}
}发布于 2022-09-06 07:16:41
我还配置页脚加载状态适配器与您的代码相同,没有任何问题。嗯,你可以在打电话给executePendingBindings()后再打给holder.binding.progressBar.isVisible = loadState is LoadState.Loading。这是onBindViewHolder() func:override fun onBindViewHolder(holder: ViewHolder, loadState: LoadState) { holder.binding.progressBar.isVisible = loadState is LoadState.Loading executePendingBindings() }
https://stackoverflow.com/questions/73612302
复制相似问题