首頁 > Java > java教程 > 主體

Android適配器(Adapter)、觀察者(Observer) 模式的程式碼分析(圖)

黄舟
發布: 2017-03-11 11:51:02
原創
3026 人瀏覽過

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

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

AdapterView 系統


  AdapterView中會使用Adapter

#Adapter 系統


  BaseAdapter 實作了SpinnerAdapter、ListAdapter

  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<DataSetObserver> {

    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<T> {

    protected final ArrayList<T> mObservers = new ArrayList<T>();

    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同理#######

以上是Android適配器(Adapter)、觀察者(Observer) 模式的程式碼分析(圖)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板