ListActivity源码分析

    最近老是和ListActivity过意不去,碰到了它的几个问题,干脆读下它的源码吧,搞清楚它的内部机制,有利于问题的解决。

    Android中的ListActivity其实就是一个自带ListView的Activity,ListActivity它位于命名空间:android.app之下,从它的源代码就可以清楚的看到,ListActivity继承了Activity,它的基本用法网上有很多资料,随便GOOGLE一下,到处都是,下面主要是分析一下它的Java源代码.

   

public class ListActivity extends Activity {

    /**

     * This field should be made private, so it is hidden from the SDK.

     * {@hide}

     */

    protected ListAdapter mAdapter;

    /**

     * This field should be made private, so it is hidden from the SDK.

     * {@hide}

     */

    protected ListView mList;



    private Handler mHandler = new Handler();

    private boolean mFinishedStart = false;



    private Runnable mRequestFocus = new Runnable() {

        public void run() {

            mList.focusableViewAvailable(mList);

        }

    };



    /**

     * This method will be called when an item in the list is selected.

     * Subclasses should override. Subclasses can call

     * getListView().getItemAtPosition(position) if they need to access the

     * data associated with the selected item.

     *

     * @param l The ListView where the click happened

     * @param v The view that was clicked within the ListView

     * @param position The position of the view in the list

     * @param id The row id of the item that was clicked

     */

    protected void onListItemClick(ListView l, View v, int position, long id) {

    }



    /**

     * Ensures the list view has been created before Activity restores all

     * of the view states.

     *

     *@see Activity#onRestoreInstanceState(Bundle)

     */

    @Override

    protected void onRestoreInstanceState(Bundle state) {

        ensureList();

        super.onRestoreInstanceState(state);

    }



    /**

     * @see Activity#onDestroy()

     */

    @Override

    protected void onDestroy() {

        mHandler.removeCallbacks(mRequestFocus);

        super.onDestroy();

    }



    /**

     * Updates the screen state (current list and other views) when the

     * content changes.

     *

     * @see Activity#onContentChanged()

     */

    @Override

    public void onContentChanged() {

        super.onContentChanged();

        View emptyView = findViewById(com.android.internal.R.id.empty);

        mList = (ListView)findViewById(com.android.internal.R.id.list);

        if (mList == null) {

            throw new RuntimeException(

                    "Your content must have a ListView whose id attribute is " +

                    "'android.R.id.list'");

        }

        if (emptyView != null) {

            mList.setEmptyView(emptyView);

        }

        mList.setOnItemClickListener(mOnClickListener);

        if (mFinishedStart) {

            setListAdapter(mAdapter);

        }

        mHandler.post(mRequestFocus);

        mFinishedStart = true;

    }



    /**

     * Provide the cursor for the list view.

     */

    public void setListAdapter(ListAdapter adapter) {

        synchronized (this) {

            ensureList();

            mAdapter = adapter;

            mList.setAdapter(adapter);

        }

    }



    /**

     * Set the currently selected list item to the specified

     * position with the adapter's data

     *

     * @param position

     */

    public void setSelection(int position) {

        mList.setSelection(position);

    }



    /**

     * Get the position of the currently selected list item.

     */

    public int getSelectedItemPosition() {

        return mList.getSelectedItemPosition();

    }



    /**

     * Get the cursor row ID of the currently selected list item.

     */

    public long getSelectedItemId() {

        return mList.getSelectedItemId();

    }



    /**

     * Get the activity's list view widget.

     */

    public ListView getListView() {

        ensureList();

        return mList;

    }



    /**

     * Get the ListAdapter associated with this activity's ListView.

     */

    public ListAdapter getListAdapter() {

        return mAdapter;

    }



    private void ensureList() {

        if (mList != null) {

            return;

        }

        setContentView(com.android.internal.R.layout.list_content_simple);



    }



    private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id)

        {

            onListItemClick((ListView)parent, v, position, id);

        }

    };

}

 代码如上,在使用ListActivity时,一般用 this.setListAdapter()传入一个ListAdapter,用来绑定ListActivity中的ListView,当调用setListAdapter()时,在ListActivity内部,会调用如下方法:

      /**
     * Provide the cursor for the list view.
     */
    public void setListAdapter(ListAdapter adapter) {
        synchronized (this) {
            ensureList();
            mAdapter = adapter;
            mList.setAdapter(adapter);
        }
    }

  其中ensureList();是一个比较重要的方法,可以查看源代码知道,mList其实就是ListActivity中的ListView了,

  接下来会调用ensureList(),先看下它是怎么弄的吧:

    private void ensureList() {
        if (mList != null) {
            return;
        }
        setContentView(com.android.internal.R.layout.list_content_simple);

    }

    可以看到,原来,就是在这里,setContentView()引入了一个名叫com.android.internal.R.layout.list_content_simple的布局资源,这个资源,就位于我们的d:\AndroidSDK\platforms\android-16\data\res\layout下的list_content_simple.xml布局文件,用ECLIPSE打开这个布局文件看下吧。

   <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="false"
    />

 原来这个ListView的ID就是android:id="@android:id/list",所以在这里如果我们要自定义LISTVIEW的布局文件时,这个LISTVIEW的ID就必须取名为:android:id="@android:id/list"啦,呵呵。

 继续往下分析,当调用   setContentView(com.android.internal.R.layout.list_content_simple)这段代码后,接下来会调用onContentChanged(),这个方法是用来干嘛的呢,看下它的英文注释: 

     /**
     * Updates the screen state (current list and other views) when the
     * content changes.
     *
     * @see Activity#onContentChanged()
     */

    原来就是当这个Activity内容发生改变时,就会调用。

   @Override
    public void onContentChanged() {
        super.onContentChanged();  //调用基类的onContentChanged
        View emptyView = findViewById(com.android.internal.R.id.empty); //查找ID为com.android.internal.R.id.empty的VIEW,这个VIEW一般用来当LISTVIEW没有任何ITEM时,显示一条信息。
        mList = (ListView)findViewById(com.android.internal.R.id.list);//查找ID为com.android.internal.R.id.list的LISTVIEW
        if (mList == null) {
            throw new RuntimeException(
                    "Your content must have a ListView whose id attribute is " +
                    "'android.R.id.list'");
        }
        if (emptyView != null) {
            mList.setEmptyView(emptyView); //setEmptyView()这个方法不是LISTACTIVITY的,而是它的父父类AdapterView的,这里的AdapterView是一个抽象类,其官方的定义是:“An AdapterView is a view whose children are determined by an Adapter.”,意思为:她是一个VIEW,什么VIEW呢?是它的子项需要一个适配器ADAPTER来填充的VIEW.


        }
        mList.setOnItemClickListener(mOnClickListener);//LISTVIEW的ITEM选中的回调方法
        if (mFinishedStart) {
            setListAdapter(mAdapter); 
        }
        mHandler.post(mRequestFocus); //Handler,这里把一个类型为Runnable的玩意儿投放到了消息队列里面,待主线程来处理。其中mRequestFocus是用来使ListView获取焦点的。
        mFinishedStart = true;
    }

 

  好了,作为一名应用程序员,对LISTACTIVITY有个这么多认识就差不多了,请“专家”和“砖家”们批评,指正,谢谢!

你可能感兴趣的:(ListActivity)