gpt4 book ai didi

java - Android DataBinding、onItemrangeMoved - 条目 block 何时在列表中移动?

转载 作者:行者123 更新时间:2023-12-02 03:29:54 26 4
gpt4 key购买 nike

我目前正在使用 Android 数据绑定(bind)库,它是 ObservableList .

但是方法onItemRangeMovedT(sender,int fromPosition, int toPosition, int itemCount)让我很困惑。

假设参数为 sender = A, B, C, D, E, fromPosition = 0, toPosition = 2, itemCount = 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,我们会发现一个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
}
}

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

关于java - Android DataBinding、onItemrangeMoved - 条目 block 何时在列表中移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38241981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com