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();
网友评论