Android DataBinding,onItemrangeMoved - 条目块何时在列表中移动?

Android DataBinding, onItemrangeMoved - When does a block of entries move within a List?

我目前正在使用 Android 的数据绑定库,它是 ObservableList

但是方法onItemRangeMovedT(sender,int fromPosition, int toPosition, int itemCount)让我很费解

假设参数为sender = A, B, C, D, EfromPosition = 0toPosition = 2itemCount = 2

据我了解,它会告诉我以下信息:

A                   C
B                   D
C   results in =>   A
D                   B
E                   E

但这是什么时候发生的? ObservableList 内部是否有一个逻辑,如果受到 Collection.sort() 或一些复杂的 Collections.rotate(l.sublist(j, k), x) 的刺激,它会监视条目的顺序并自动调用 onItemRangeMoved东西,甚至跟踪组,例如。 [ab]cde -> cd[ab]e?

itemCount 参数真让我感到奇怪。

来自 documentation:

void onItemRangeMoved (T sender, // The changing list.
            int fromPosition,    // The position from which the items were moved.
            int toPosition,      // The destination position of the items.
            int itemCount)       // The number of items moved.

Called whenever items in the list have been moved.


通过 ObservableArrayList.class 挖掘,你会发现一个延伸 ObservableList.OnListChangedCallbackListChangeRegistry.class。从 ObservableArrayList.class 你可以找到像 set():

这样的方法
// mListener is an instance of ListChangeRegistry
@Override
public T set(int index, T object) {
    T val = super.set(index, object);
    if (mListeners != null) {
        mListeners.notifyChanged(this, index, 1);
    }
    return val;
}

这是调用的方法:

/**
 * Notify registered callbacks that some elements have changed.
 *
 * @param list The list that changed.
 * @param start The index of the first changed element.
 * @param count The number of changed elements.
 */
public void notifyChanged(ObservableList list, int start, int count) {
    ListChanges listChanges = acquire(start, 0, count);
    notifyCallbacks(list, CHANGED, listChanges);
}

调用:

 @Override
 public void onNotifyCallback(ObservableList.OnListChangedCallback callback,
            ObservableList sender, int notificationType, ListChanges listChanges) {
        switch (notificationType) {
            case CHANGED:
                callback.onItemRangeChanged(sender, listChanges.start, listChanges.count);
                break;
            //more cases
        }
}

由于更改、移动等有时需要 set 元素,我猜 onItemRangeChanged 每次都会触发 - 正如文档所述。