Android开源代码解读の地图照片应用Panoramio的实现详解(四)
本文继续讲解Panoramio的实现,主要介绍ImageAdapter.java和ImageList.java 这两个文件,这两个文件实现了如下所示的界面,左图是数据从网络加载过程中,有图是加载完成后的效果:
ImageAdapter继承自BaseAdapter类,实现图片适配器的功能,而ImageList则继承自ListActivity,用于以列表形式显示搜索到的图片信息。介绍之前,先来普及下DataSetObserver和DataSetObservable的知识。
从名字可以依稀猜到,DataSetObserver实现了观察者模式中的观察者角色(Observer)。当数据集发生变化或者变为无效时,DataSetObserver中的方法被回调,典型的数据集有Cursor和Adapter,当某个对象要添加到DataSetObservable中时,这个对象必须从DataSetObserver继承,DataSetObserver是一个抽象类,定义如下:
[java]
public abstract class DataSetObserver {
* This method is called when the entire data set has changed,
* most likely through a call to {@link Cursor#requery()} on a {@link Cursor}.
*/
public void onChanged() {
// Do nothing
}
/**
* This method is called when the entire data becomes invalid,
* most likely through a call to {@link Cursor#deactivate()} or {@link Cursor#close()} on a
* {@link Cursor}.
*/
public void onInvalidated() {
// Do nothing
}
}
public abstract class DataSetObserver {
/**
* This method is called when the entire data set has changed,
* most likely through a call to {@link Cursor#requery()} on a {@link Cursor}.
*/
public void onChanged() {
// Do nothing
}
/**
* This method is called when the entire data becomes invalid,
* most likely through a call to {@link Cursor#deactivate()} or {@link Cursor#close()} on a
* {@link Cursor}.
*/
public void onInvalidated() {
// Do nothing
}
}
而DataSetObservable实现了观察者模式中的对象角色(Subject),它是Observable的具体实现,提供了调用DataSetObserver中各种回调函数的方法,定义如下所示:
[java]
public class DataSetObservable extends Observable<DataSetObserver> {
/**
* Invokes onChanged on each observer. Called when the data set being observed has
* changed, and which when read contains the new state of the data.
*/
public void notifyChanged() {
synchronized(mObservers) {
for (DataSetObserver observer : mObservers) {
observer.onChanged();
}
}
}
/**
* Invokes onInvalidated on each observer. Called when the data set being monitored
* has changed such that it is no longer valid.
*/
public void notifyInvalidated() {
synchronized (mObservers) {
for (DataSetObserver observer : mObservers) {
observer.onInvalidated();
}
}
}
}
public class DataSetObservable extends Observable<DataSetObserver> {
/**
* Invokes onChanged on each observer. Called when the data set being observed has
* changed, and which when read contains the new state of the data.
*/
public void notifyChanged() {
synchronized(mObservers) {
for (DataSetObserver observer : mObservers) {
observer.onChanged();
}
}
}
/**
* Invokes onInvalidated on each observer. Called when the data set being monitored
* has changed such that it is no longer valid.
*/
public void notifyInvalidated() {
synchronized (mObservers) {
for (DataSetObserver observer : mObservers) {
observer.onInvalidated();
}
}
}
}
注意这里的Observable是定义在android.database包中的,而不是JDK中的Observable,它的定义如下:
[java]
package android.database;
import java.util.ArrayList;
/**
* Provides methods for (un)registering arbitrary observers in an ArrayList.
*/
public abstract class Observable<T> {
/**
* The list of observers. An observer can be in the list at most
* once and will never be null.
*/
protected final ArrayList<T> mObservers = new ArrayList<T>();
/**
* Adds an observer to the list. The observer cannot be null and it must not already
* be registered.
* @param observer the observer to register
* @throws IllegalArgumentException the observer is null
补充:移动开发 , Android ,