android批量删除图片,Android RecyclerView单点、批量数据元素项目item的增加、删除和移动...

Android RecyclerView单点、批量数据元素项目item的增加、删除和移动

前文附录1,2介绍了基本的Android RecyclerView单点、批量元素项目的更新。现在给出其他比较重要的Android RecyclerView数据元素项目的删除和增加,删除和增加包含两种,一种是单点,另外一种是批量的元素。

(一)RecyclerView删除操作。

(a)单点删除:notifyItemRemoved(int position)

/**

* Notify any registered observers that the item previously located at position

* has been removed from the data set. The items previously located at and after

* position may now be found at oldPosition - 1.

*

*

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.

*

* @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 position

* has been newly inserted. The item previously at position is now at

* position position + 1.

*

*

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.

*

* @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 fromPosition

* has been moved to toPosition.

*

*

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.

*

* @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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值