Android RecyclerView单点、批量数据元素项目item的增加、删除和移动
前文附录1,2介绍了基本的Android RecyclerView单点、批量元素项目的更新。现在给出其他比较重要的Android RecyclerView数据元素项目的删除和增加,删除和增加包含两种,一种是单点,另外一种是批量的元素。
(一)RecyclerView删除操作。
(a)单点删除:notifyItemRemoved(int position)
/**
* Notify any registered observers that the item previously located at <code>position</code>
* has been removed from the data set. The items previously located at and after
* <code>position</code> may now be found at <code>oldPosition - 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their positions
* may be altered.</p>
*
* @param position Position of the item that has now been removed
*
* @see #notifyItemRangeRemoved(int, int)
*/
public final void notifyItemRemoved(int position) {
mObservable.notifyItemRangeRemoved(position, 1);
}
在给adapter维持的数据队列删除一个元素后,调用此方法,该方法删除指定位置position的元素并更新RecyclerView。
(b)批量删除:notifyItemRangeRemoved(int positionStart, int itemCount)
public void notifyItemRangeRemoved(int positionStart, int itemCount) {
// since onItemRangeRemoved() is implemented by the app, it could do anything, including
// removing itself from {@link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {@link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onItemRangeRemoved(positionStart, itemCount);
}
}
在给adapter维持的数据元素队列批量删除若干元素后,调用该方法,该方法删除从开始位置positionStart起之后的itemCount个数量的子元素,然后更新RecyclerView。
(二)RecyclerView增加操作。
(a)单点增加:notifyItemInserted(int position)
/**
* Notify any registered observers that the item reflected at <code>position</code>
* has been newly inserted. The item previously at <code>position</code> is now at
* position <code>position + 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param position Position of the newly inserted item in the data set
*
* @see #notifyItemRangeInserted(int, int)
*/
public final void notifyItemInserted(int position) {
mObservable.notifyItemRangeInserted(position, 1);
}
在给adapter指定位置position增加一个元素后,调用notifyItemInserted方法,更新RecyclerView。
(b)批量增加:notifyItemRangeInserted(int positionStart, int itemCount)
public void notifyItemRangeInserted(int positionStart, int itemCount) {
// since onItemRangeInserted() is implemented by the app, it could do anything,
// including removing itself from {@link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {@link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
for (int i = mObservers.size() - 1; i >= 0; i--) {
mObservers.get(i).onItemRangeInserted(positionStart, itemCount);
}
}
在给adapter适配器指定位置positionStart增加itemCount个数据元素后,调用此方法,更新RecyclerView。
(三)RecyclerView移动操作。
notifyItemMoved(int fromPosition, int toPosition)
/**
* Notify any registered observers that the item reflected at <code>fromPosition</code>
* has been moved to <code>toPosition</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param fromPosition Previous position of the item.
* @param toPosition New position of the item.
*/
public final void notifyItemMoved(int fromPosition, int toPosition) {
mObservable.notifyItemMoved(fromPosition, toPosition);
}
移动操作的对象位置有两个,开始对象位置fromPosition和目的地址位置toPosition。设定这两个位置后,fromPosition元素位置的元素被移动到toPosition。fromPosition和toPosition是元素的集合队列中的下标。
附录:
1,《 Android RecyclerView更新子项目notifyItemChanged》链接:http://blog.csdn.net/zhangphil/article/details/78565738
2,《Android RecyclerView批量更新notifyItemRangeChanged》链接:http://blog.csdn.net/zhangphil/article/details/78579849