适配器(Adapter)详述:http://www.php.cn/java-article-355851.html

观察者(Observer)详述:http://www.php.cn/java-article-356024.html

AdapterView 体系

556bce00a2d9826e54dab7af6afb3022.png

AdapterView中会使用Adapter

Adapter 体系

29ad17c14c7eb67740e60cf65e175a4a.png

BaseAdapter 实现了 SpinnerAdapter、ListAdapter

这样的形式,就是 适配器模式的应用, 这里是采用的是 接口适配器

观察者(Observer) 模式

在Adapter中定义了注册和反注册DataSetObserver的方法public interface Adapter {

void registerDataSetObserver(DataSetObserver observer);

void unregisterDataSetObserver(DataSetObserver observer);

...

}public abstract class DataSetObserver {

public void onChanged() {

// Do nothing

}

public void onInvalidated() {

// Do nothing

}

}public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {

private final DataSetObservable mDataSetObservable = new DataSetObservable();

public void registerDataSetObserver(DataSetObserver observer) {

mDataSetObservable.registerObserver(observer);

}

public void unregisterDataSetObserver(DataSetObserver observer) {

mDataSetObservable.unregisterObserver(observer);

}

public void notifyDataSetChanged() {

mDataSetObservable.notifyChanged();

}

public void notifyDataSetInvalidated() {

mDataSetObservable.notifyInvalidated();

}

...

}public class DataSetObservable extends Observable {

public void notifyChanged() {

synchronized(mObservers) {

for (int i = mObservers.size() - 1; i >= 0; i--) {

mObservers.get(i).onChanged();

}

}

}

public void notifyInvalidated() {

synchronized (mObservers) {

for (int i = mObservers.size() - 1; i >= 0; i--) {

mObservers.get(i).onInvalidated();

}

}

}

}public abstract class Observable {

protected final ArrayList mObservers = new ArrayList();

public void registerObserver(T observer) {

if (observer == null) {

throw new IllegalArgumentException("The observer is null.");

}

synchronized(mObservers) {

if (mObservers.contains(observer)) {

throw new IllegalStateException("Observer " + observer + " is already registered.");

}

mObservers.add(observer);

}

}

public void unregisterObserver(T observer) {

if (observer == null) {

throw new IllegalArgumentException("The observer is null.");

}

synchronized(mObservers) {

int index = mObservers.indexOf(observer);

if (index == -1) {

throw new IllegalStateException("Observer " + observer + " was not registered.");

}

mObservers.remove(index);

}

}

public void unregisterAll() {

synchronized(mObservers) {

mObservers.clear();

}

}

}

DataSetObserver 观察者(观察数据集:发生变化onChanged、无效onInvalidated)

Observable 处理观察者(添加、移除、注册、反注册)

DataSetObservable extends Observable 添加了通知数据改变和无效的 两个方法

在AdapterView中class AdapterDataSetObserver extends DataSetObserver {

private Parcelable mInstanceState = null;

@Override

public void onChanged() {

mDataChanged = true;

mOldItemCount = mItemCount;

mItemCount = getAdapter().getCount();

// Detect the case where a cursor that was previously invalidated has

// been repopulated with new data.

if (AdapterView.this.getAdapter().hasStableIds() && mInstanceState != null

&& mOldItemCount == 0 && mItemCount > 0) {

AdapterView.this.onRestoreInstanceState(mInstanceState);

mInstanceState = null;

} else {

rememberSyncState();

}

checkFocus();

requestLayout();

}

@Override

public void onInvalidated() {

mDataChanged = true;

if (AdapterView.this.getAdapter().hasStableIds()) {

// Remember the current state for the case where our hosting activity is being

// stopped and later restarted

mInstanceState = AdapterView.this.onSaveInstanceState();

}

// Data is invalid so we should reset our state

mOldItemCount = mItemCount;

mItemCount = 0;

mSelectedPosition = INVALID_POSITION;

mSelectedRowId = INVALID_ROW_ID;

mNextSelectedPosition = INVALID_POSITION;

mNextSelectedRowId = INVALID_ROW_ID;

mNeedSync = false;

checkFocus();

requestLayout();

}

public void clearSavedState() {

mInstanceState = null;

}

}

自定义了AdapterDataSetObserver,在两个实现方法中都调用了requestLayout(),重新设置layout

整个观察数据变化导致界面改变的流程:

1. 在AdapterView体系中 自定义 AdapterDataSetObserver,并注册(即添加observer到observable)。

2. 在调用adapter.notifyDataSetChanged()后,内部执行observable的notifyChanged(),

将执行每一个observer的onChanged(),也就达到了更新界面的效果。

notifyDataSetInvalidated同理

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐