自编监听器的一种实现模式

ConversationListAdapter.java  extends  CursorAdapter  implements AbslistView.RecyclerListener{

    ...

       public ConversationListAdapter(Context context, Cursor cursor) {
        super(context, cursor, false /* auto-requery */);
        mFactory = LayoutInflater.from(context);
    }

       public interface OnContentChangedListener {                       //定义一个内容变化监听器
        void onContentChanged(ConversationListAdapter adapter);
    }

       public void setOnContentChangedListener(OnContentChangedListener l) {
        mOnContentChangedListener = l;
    }

/ ** onContentChanged来自于curserAdapter.java   注释如下

 /**
     * Called when the {@link ContentObserver} on the cursor receives a change notification.
     * The default implementation provides the auto-requery logic, but may be overridden by
     * sub classes.
     *
     * @see ContentObserver#onChange(boolean)
     */

    protected void onContentChanged() {
        if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {
            if (Config.LOGV) Log.v("Cursor", "Auto requerying " + mCursor + " due to update");
            mDataValid = mCursor.requery();
        }
    }


  /**
     * Change the underlying cursor to a new cursor. If there is an existing cursor it will be
     * closed.
     *
     * @param cursor the new cursor to be used
     */
    public void changeCursor(Cursor cursor) {
        if (cursor == mCursor) {
            return;
        }
        if (mCursor != null) {
            mCursor.unregisterContentObserver(mChangeObserver);
            mCursor.unregisterDataSetObserver(mDataSetObserver);
            mCursor.close();
        }
        mCursor = cursor;
        if (cursor != null) {
            cursor.registerContentObserver(mChangeObserver);
            cursor.registerDataSetObserver(mDataSetObserver);
            mRowIDColumn = cursor.getColumnIndexOrThrow("_id");
            mDataValid = true;
            // notify the observers about the new cursor
            notifyDataSetChanged();
        } else {
            mRowIDColumn = -1;
            mDataValid = false;
            // notify the observers about the lack of a data set
            notifyDataSetInvalidated();
        }
    }


*/
     @Override
      protected void onContentChanged() {
          if (mCursor != null && !mCursor.isClosed()) {
              if (mOnContentChangedListener != null) {
                mOnContentChangedListener.onContentChanged(this);            
            }
        }
    }

}

在  ConversationList extends ListActivity{

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...

        initListAdapter();

        ...

      }

      private void initListAdapter() {
        mListAdapter = new ConversationListAdapter(this, null);
        mListAdapter.setOnContentChangedListener(mContentChangedListener);         //mlistadapter 给自己设置 监听器,当curser的contentObserver 发现curser改变      时,OnContentChangedListener 中的方法将会被调用
        setListAdapter(mListAdapter);
        getListView().setRecyclerListener(mListAdapter);
    }

}




你可能感兴趣的:(自编监听器的一种实现模式)