美文网首页
Recyclerview-局部刷新

Recyclerview-局部刷新

作者: Method | 来源:发表于2021-07-11 16:57 被阅读0次

    Recyclerview 无脑刷新

    notifyDataSetChanged

    notifyDataSetChanged会更新全部列表

     mAdapter?.apply {
                setData(data)
                notifyDataSetChanged()
            }
    
    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        "position $position --->update".e()
        val userBean = mData[position]
        holder.binding.mIdTv.text = userBean.id.toString()
        holder.binding.mNameTv.text = userBean.firstName+" "+userBean.lastName
    }    
    
    normal.gif

    Recyclerview 局部刷新

    notifyItemInserted

    添加item到某个位置,并单独刷新

    private fun insertedData() {
            val userBean = UserBean(20,"inserted data","")
            data.add(2,userBean)
            mAdapter?.notifyItemInserted(2)
        }
    
    inserted.gif

    notifyItemRemoved

    删除某个位置,并刷新

    private fun removeData() {
            data.removeAt(5)
            mAdapter?.notifyItemRemoved(5)
        }
    

    remove并不会回调onBindViewHolder方法


    remove.gif

    notifyItemChanged

    更新某个位置并刷新

    private fun updateData() {
            data.set(8, UserBean(8,"LILI","--USA"))
            mAdapter?.notifyItemChanged(8)
        }
    
    update.gif

    区间更新

    adapter.notifyItemRangeChanged();
    adapter.notifyItemRangeInserted();
    adapter.notifyItemRangeRemoved();
    

    相关文章

      网友评论

          本文标题:Recyclerview-局部刷新

          本文链接:https://www.haomeiwen.com/subject/lodjsltx.html