android批量删除图片,Android RecyclerView单点、批量数据元素项目item的增加、删除和移动...
Android RecyclerView單點(diǎn)、批量數(shù)據(jù)元素項(xiàng)目item的增加、刪除和移動
前文附錄1,2介紹了基本的Android RecyclerView單點(diǎn)、批量元素項(xiàng)目的更新。現(xiàn)在給出其他比較重要的Android RecyclerView數(shù)據(jù)元素項(xiàng)目的刪除和增加,刪除和增加包含兩種,一種是單點(diǎn),另外一種是批量的元素。
(一)RecyclerView刪除操作。
(a)單點(diǎn)刪除: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維持的數(shù)據(jù)隊(duì)列刪除一個元素后,調(diào)用此方法,該方法刪除指定位置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維持的數(shù)據(jù)元素隊(duì)列批量刪除若干元素后,調(diào)用該方法,該方法刪除從開始位置positionStart起之后的itemCount個數(shù)量的子元素,然后更新RecyclerView。
(二)RecyclerView增加操作。
(a)單點(diǎn)增加: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增加一個元素后,調(diào)用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個數(shù)據(jù)元素后,調(diào)用此方法,更新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。設(shè)定這兩個位置后,fromPosition元素位置的元素被移動到toPosition。fromPosition和toPosition是元素的集合隊(duì)列中的下標(biāo)。
附錄:
1,《 Android RecyclerView更新子項(xiàng)目notifyItemChanged》鏈接:http://blog.csdn.net/zhangphil/article/details/78565738
2,《Android RecyclerView批量更新notifyItemRangeChanged》鏈接:http://blog.csdn.net/zhangphil/article/details/78579849
總結(jié)
以上是生活随笔為你收集整理的android批量删除图片,Android RecyclerView单点、批量数据元素项目item的增加、删除和移动...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用html标记语言,HTML标记语言——
- 下一篇: html hover图片效果,CSS第9