gpt4 book ai didi

android - 使用 GridLayoutManager 在分页库中设置跨度大小

转载 作者:行者123 更新时间:2023-12-04 11:55:21 29 4
gpt4 key购买 nike

GridLayoutManager 中将跨度大小设置为 1当LoadStateLoading在分页库中。
我试过this解决方案,但它不起作用。

对于复制问题:克隆 this官方 repo 和设置GridLayoutManagerSearchRepositoriesActivity
我的代码在这里

电影列表 fragment

....
private val adapter = MoviesAdapter()
....
private fun initAdapter() {
val decoration = DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
binding.rvMovies.addItemDecoration(decoration)
binding.rvMovies.layoutManager = GridLayoutManager(requireContext(), 2)
binding.rvMovies.isNestedScrollingEnabled = true
binding.rvMovies.adapter = adapter.withLoadStateFooter(
footer = MoviesLoadStateAdapter { adapter.retry() }
)
}

电影适配器
class MoviesAdapter(
) : PagingDataAdapter<Movie, MoviesAdapter.ViewHolder>(MOVIE_COMPARATOR) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = ItemMovieListBinding.inflate(LayoutInflater.from(parent.context))
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val movie = getItem(position)
if (movie != null) {
val viewModel = MovieListItemViewModel(movie)
holder.bind(viewModel)
}
}

inner class ViewHolder(var viewBinding: ItemMovieListBinding) :
RecyclerView.ViewHolder(viewBinding.root) {
fun bind(viewModel: MovieListItemViewModel) {
viewBinding.viewModel = viewModel

}
}

companion object {
private val MOVIE_COMPARATOR = object : DiffUtil.ItemCallback<Movie>() {
override fun areItemsTheSame(oldItem: Movie, newItem: Movie): Boolean =
oldItem.id == newItem.id

override fun areContentsTheSame(oldItem: Movie, newItem: Movie): Boolean =
oldItem == newItem
}
}
}

MoviesLoadStateAdapter
class MoviesLoadStateAdapter(private val retry : () -> Unit) :
LoadStateAdapter<MoviesLoadStateAdapter.MoviesLoadStateViewHolder>()
{

override fun onBindViewHolder(holder: MoviesLoadStateViewHolder, loadState: LoadState) {
holder.bind(loadState)
}

override fun onCreateViewHolder(
parent: ViewGroup,
loadState: LoadState
): MoviesLoadStateViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_movie_load_state_footer_view, parent, false)
val binding = ItemMovieLoadStateFooterViewBinding.bind(view)
return MoviesLoadStateViewHolder(binding, retry)

}

inner class MoviesLoadStateViewHolder(private val binding: ItemMovieLoadStateFooterViewBinding,
retry : ()-> Unit) :
RecyclerView.ViewHolder(binding.root) {
init {
binding.retryButton.setOnClickListener {
retry.invoke()
}
}
fun bind(loadState : LoadState){
if(loadState is LoadState.Error){
binding.errorMsg.text = loadState.error.localizedMessage
}
binding.progressBar.visibility = toVisibility(loadState is LoadState.Loading)
}
}
}

这里的目标是 ProgressBar应该显示在屏幕中央。

enter image description here

最佳答案

我解决了。这是我的代码
在您的 Activity 中

val gridLayoutManager = GridLayoutManager(activity, 2)
gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
val viewType = wallpaperAdapter.getItemViewType(position)
return if(viewType == WALLPAPER_VIEW_TYPE) 1
else 2
}
}

binding.recyclerView.apply {
this.layoutManager = gridLayoutManager
this.setHasFixedSize(true)
this.adapter = wallpaperAdapter
}
在您的 PagingDataAdapter
override fun getItemViewType(position: Int): Int {
if (position == itemCount){
return NETWORK_VIEW_TYPE
}else {
return WALLPAPER_VIEW_TYPE
}
}
希望它对你有用。

关于android - 使用 GridLayoutManager 在分页库中设置跨度大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358002/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com