Android DataBinding,onItemrangeMoved - 一个条目块何时在List中移动?

时间:2016-07-07 09:29:35

标签: java android list collections android-databinding

我目前正在使用Androids数据绑定库及其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]è?

itemCount参数确实让我感到沮丧。

1 个答案:

答案 0 :(得分:2)

来自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.
  

每当移动列表中的项目时调用。

通过ObservableArrayList.class挖掘,会发现ListChangeRegistry.class扩展ObservableList.OnListChangedCallback。从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 - 正如文档所述。