RecyclerView刷新方式

RecyclerView刷新方式

刷新全部item

  • notifyDataSetChanged()
student.setValue(new Student("二狗"));
studentList.add(student.getValue());
myRecyclerViewAdapter.notifyDataSetChanged();

刷新指定item

  • notifyItemChanged(int)
studentList.get(1).setName("铁蛋");
myRecyclerViewAdapter.notifyItemChanged(1);

从指定位置开始刷新指定个数的item

  • notifyItemRangeChanged(int,int)
Student s = new Student("二狗");
studentList.add(1, s);
myRecyclerViewAdapter.notifyItemInserted(1);
myRecyclerViewAdapter.notifyItemRangeChanged(1, studentList.size() + 1);

插入、移动指定位置的item并刷新

  • notifyItemInserted(int)、notifyItemMoved(int)、notifyItemRemoved(int)
// 先移下标大的
Student s2 = studentList.remove(4);
Student s1 = studentList.remove(1);
// 先添加下标小的
studentList.add(1, s2);
studentList.add(4, s1);
// 带动画的移动
myRecyclerViewAdapter.notifyItemMoved(4,1);
// 受到影响的item都要刷新
myRecyclerViewAdapter.notifyItemRangeChanged(1,4);

局部刷新指定item

  • notifyItemChanged(int, Object)
Student s = new Student("二狗");
studentList.set(1, s);
myRecyclerViewAdapter.notifyItemChanged(1, s);
posted @ 2021-10-06 12:38  n1ce2cv  阅读(543)  评论(0编辑  收藏  举报