我目前正在使用Androids数据绑定库及其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]è?
itemCount
参数确实让我感到沮丧。
答案 0 :(得分:2)
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
- 正如文档所述。