AbsListView 内部定义了:
class AdapterDataSetObserver extends AdapterView<ListAdapter>.AdapterDataSetObserver {
@Override
public void onChanged() {
super.onChanged();
if (mFastScroll != null) {
mFastScroll.onSectionsChanged();
}
}
@Override
public void onInvalidated() {
super.onInvalidated();
if (mFastScroll != null) {
mFastScroll.onSectionsChanged();
}
}
}
AttachedToWindow的时候,创建Observer,并添注册该观察者,
该用户用于刷新,mFastScroll.onSectionsChanged();
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
...........................
if (mAdapter != null && mDataSetObserver == null) {
mDataSetObserver = new AdapterDataSetObserver();
mAdapter.registerDataSetObserver(mDataSetObserver);
// Data may have changed while we were detached. Refresh.
mDataChanged = true;
mOldItemCount = mItemCount;
mItemCount = mAdapter.getCount();
}
}
什么时候调用了?
当我们调用BaseAdapter.notifyDataSetChanged的时候
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
public void notifyChanged() {
synchronized(mObservers) {
// since onChanged() 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).onChanged();
}
}
}
就会回调到上面在AbsListView 内部类中的AdapterDataSetObserver 的
onChanged方法中。