天天看點

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 

繼續閱讀