Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Recyclerview如果设置StaggeredGridLayoutManager和GridLayoutManager时,item复用功能失效 #6511

Open
a1227308812 opened this issue Oct 24, 2024 · 0 comments

Comments

@a1227308812
Copy link

Describe the bug
当Recyclerview设置为StaggeredGridLayoutManager和GridLayoutManager时,item的复用机制就失效了,新增的数据都会调用onCreateViewHolder方法重新渲染布局,设置LinearLayoutmanager就不会再新增数据时一直调用onCreateViewHolder。

brvah版本:4.1.4

产生的问题:数据过多时列表开始卡顿

下面是两个adapter的比较代码,其他代码是一致的,就改了adapter的代码

/** 原生adapter **/
class ListAdapter : RecyclerView.Adapter<ListAdapter.MyViewHolder>() {
val dataList = mutableListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
LogUtil.e("onCreateViewHolder")
val binding = ItemVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return MyViewHolder(binding)
}

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = dataList[position]
        holder.binding.ivIcon.loadImage(item.img) //这里不要传圆角,传了加载gif时不会播放gif
        holder.binding.tvTitle.text = item.title
        holder.binding.tvAuthorName.text = item.author_name
        holder.binding.ivAuthor.loadImage(item.author_header)

        holder.binding.root.viewClick {
            clickListener?.invoke(this, holder.binding.root, position)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    fun submitList(list: List<VideoInfo>) {
        dataList.clear()
        dataList.addAll(list)
        notifyDataSetChanged()
    }

    fun addAll(list: List<VideoInfo>) {
        dataList.addAll(list)
        notifyItemRangeInserted(dataList.size - list.size, list.size)
    }

    private var clickListener:((adapter: RecyclerView.Adapter<ListAdapter.MyViewHolder>, view: View, position: Int) -> Unit)?=null
    fun setOnDebouncedItemClick(block: (adapter: RecyclerView.Adapter<ListAdapter.MyViewHolder>, view: View, position: Int) -> Unit) {
        clickListener = block
    }

    class MyViewHolder(val binding: ItemVideoBinding) : RecyclerView.ViewHolder(binding.root){

    }
}

/** brvah **/
// private class ListAdapter : BaseQuickAdapter<VideoInfo, ListAdapter.VH>() {
// class VH(
// parent: ViewGroup,
// val binding: ItemVideoBinding = ItemVideoBinding.inflate(
// LayoutInflater.from(parent.context), parent, false
// ),
// ) : RecyclerView.ViewHolder(binding.root)
//
// override fun getItemViewType(position: Int, list: List): Int {
// return position
// }
//
// override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
// LogUtil.e("onCreateViewHolder")
// return VH(parent)
// }
//
//
// override fun onBindViewHolder(holder: VH, position: Int, item: VideoInfo?) {
// item ?: return
// holder.binding.run {
//// ivIcon.loadVideoFrameImage(item.img, 10f)
// ivIcon.loadImage(item.img) //这里不要传圆角,传了加载gif时不会播放gif
// tvTitle.text = item.title
// tvAuthorName.text = item.author_name
// ivAuthor.loadImage(item.author_header)
// }
// }
// }

@a1227308812 a1227308812 changed the title Recyclerview的item复用功能失效 Recyclerview如果设置StaggeredGridLayoutManager和GridLayoutManager时,item复用功能失效 Oct 24, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant