1. 程式人生 > >Android RecyclerView更新某條/一條資料

Android RecyclerView更新某條/一條資料

注意:使用notifyItemRemoved(position)更新資料之前,還必須集合中刪除該資料:mDatas.remove(position),否則不更新。

更新資料 

這裡更新資料集不是用adapter.notifyDataSetChanged()而是 notifyItemInserted(position)與notifyItemRemoved(position) 否則沒有動畫效果。  

為adapter中新增兩個方法:

public void addData(int position) {
    mDatas.add(position, "Insert One");
    notifyItemInserted(position);
}
 
public void removeData(int position) {
    mDatas.remove(position);
    notifyItemRemoved(position);
}

下面方法更好:

http://blog.csdn.net/jdsjlzx/article/details/52893469