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 <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 
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,如果要进行RecyclerView批量操作,可以通过以下几个步骤来实现。 首先,要创建一个自定义的RecyclerView适配器。在适配器中,我们可以添加一个boolean类型的数据成员,用于标记当前RecyclerView批量操作状态。我们可以在适配器的构造方法中将该boolean类型的成员变量初始化为false。 其次,我们可以在RecyclerView的布局文件中添加一个全选的CheckBox。当用户点击全选CheckBox时,我们需要遍历RecyclerView的所有item并将其标记为选中状态,同时更新item对应的数据模型,使其选中状态为true。再次点击全选CheckBox时,我们需要取消所有item的选中状态。 然后,我们可以在RecyclerViewitem布局文件中添加一个CheckBox来实现单个item的选中。我们可以通过监听CheckBox的点击事件,来更新item的选中状态,并刷新RecyclerView。 接下来,我们可以在RecyclerView的Activity或Fragment中创建一个菜单栏。在该菜单栏中,可以添加批量操作的选项,比如删除选中的item。当用户选择了批量操作的选项后,我们需要遍历RecyclerView的所有item,找到选中的item,并进行相应的批量操作。 最后,在RecyclerView适配器中增加一个方法,用来获取选中的item的位置。通过这个方法,我们可以获取选中的item的位置,进而获取选中的item数据,并进行相应的批量操作。 通过以上的步骤,我们可以实现RecyclerView批量操作,让用户能够方便地进行全选、取消全选、单个item选中和批量操作的功能。这样可以提高用户的使用体验,并方便用户进行操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值