Android列表中元素的增加和删除
1.列表增加元素需要注意:
mJokeAdapter.notifyItemInserted(int)
mJokeAdapter.notifyItemRangeInserted(positionStart, data.size)
2.列表中删除元素需要注意:
list.remove(i);
notifyItemRemoved(i);
notifyItemRangeChanged(i, list.size() - 1);
3.使用ListAdapter增加元素和删除元素:
listAdapter.submitList(mList);
notifyItemRemoved(i);
notifyItemRangeChanged(i, list.size() - 1);
3.解决更新数据的时候,列表的Item出现闪烁的问题:
01.使用局部刷新
adapter.notifyItemChanged(position);
02.设置adapter的属性
adapter.setHasStableIds(true);
03.给Item的position标记tag
@Override
public long getItemId(int position) {
return position;
}
|