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 

更多相关文章

  1. Android(安卓)使用LayerDrawable自定制SeekBar的外观
  2. Android(安卓)parse XML
  3. android基本组件介绍
  4. listview的滑动删除框架的使用
  5. Android:INSTALL_FAILED_UPDATE_INCOMPATIBLE 错误解决方法
  6. 单点登录(三)| JIRA 安装及 JIRA 集成 CAS 实践
  7. RelativeLayout常用属性介绍及实例讲解
  8. Android(安卓)lint 自动检测并删除无用资源
  9. android framework增加/删除类

随机推荐

  1. 学习Android开发,-----------配置环境,
  2. Android:防止过快点击造成多次事件
  3. Android实现三级联动下拉框 下拉列表spin
  4. Android多媒体相关框架
  5. 可以通过在AndroidManifest.xml中指定Act
  6. Android的线程使用来更新UI----Thread、H
  7. Android(安卓)unspecified' depends on o
  8. Android(安卓)动画
  9. 自定义Seekbar
  10. android 文件上传的类--完整 可以直接被