ListActivity源码分析

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

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

  1 public class ListActivity extends Activity {
  2     /**
  3      * This field should be made private, so it is hidden from the SDK.
  4      * {@hide}
  5      */
  6     protected ListAdapter mAdapter;
  7     /**
  8      * This field should be made private, so it is hidden from the SDK.
  9      * {@hide}
 10      */
 11     protected ListView mList;
 12  
 13     private Handler mHandler = new Handler();
 14     private boolean mFinishedStart = false;
 15  
 16     private Runnable mRequestFocus = new Runnable() {
 17         public void run() {
 18             mList.focusableViewAvailable(mList);
 19         }
 20     };
 21  
 22     /**
 23      * This method will be called when an item in the list is selected.
 24      * Subclasses should override. Subclasses can call
 25      * getListView().getItemAtPosition(position) if they need to access the
 26      * data associated with the selected item.
 27      *
 28      * @param l The ListView where the click happened
 29      * @param v The view that was clicked within the ListView
 30      * @param position The position of the view in the list
 31      * @param id The row id of the item that was clicked
 32      */
 33     protected void onListItemClick(ListView l, View v, int position, long id) {
 34     }
 35  
 36     /**
 37      * Ensures the list view has been created before Activity restores all
 38      * of the view states.
 39      *
 40      *@see Activity#onRestoreInstanceState(Bundle)
 41      */
 42     @Override
 43     protected void onRestoreInstanceState(Bundle state) {
 44         ensureList();
 45         super.onRestoreInstanceState(state);
 46     }
 47  
 48     /**
 49      * @see Activity#onDestroy()
 50      */
 51     @Override
 52     protected void onDestroy() {
 53         mHandler.removeCallbacks(mRequestFocus);
 54         super.onDestroy();
 55     }
 56  
 57     /**
 58      * Updates the screen state (current list and other views) when the
 59      * content changes.
 60      *
 61      * @see Activity#onContentChanged()
 62      */
 63     @Override
 64     public void onContentChanged() {
 65         super.onContentChanged();
 66         View emptyView = findViewById(com.android.internal.R.id.empty);
 67         mList = (ListView)findViewById(com.android.internal.R.id.list);
 68         if (mList == null) {
 69             throw new RuntimeException(
 70                     "Your content must have a ListView whose id attribute is " +
 71                     "'android.R.id.list'");
 72         }
 73         if (emptyView != null) {
 74             mList.setEmptyView(emptyView);
 75         }
 76         mList.setOnItemClickListener(mOnClickListener);
 77         if (mFinishedStart) {
 78             setListAdapter(mAdapter);
 79         }
 80         mHandler.post(mRequestFocus);
 81         mFinishedStart = true;
 82     }
 83  
 84     /**
 85      * Provide the cursor for the list view.
 86      */
 87     public void setListAdapter(ListAdapter adapter) {
 88         synchronized (this) {
 89             ensureList();
 90             mAdapter = adapter;
 91             mList.setAdapter(adapter);
 92         }
 93     }
 94  
 95     /**
 96      * Set the currently selected list item to the specified
 97      * position with the adapter's data
 98      *
 99      * @param position
100      */
101     public void setSelection(int position) {
102         mList.setSelection(position);
103     }
104  
105     /**
106      * Get the position of the currently selected list item.
107      */
108     public int getSelectedItemPosition() {
109         return mList.getSelectedItemPosition();
110     }
111  
112     /**
113      * Get the cursor row ID of the currently selected list item.
114      */
115     public long getSelectedItemId() {
116         return mList.getSelectedItemId();
117     }
118  
119     /**
120      * Get the activity's list view widget.
121      */
122     public ListView getListView() {
123         ensureList();
124         return mList;
125     }
126  
127     /**
128      * Get the ListAdapter associated with this activity's ListView.
129      */
130     public ListAdapter getListAdapter() {
131         return mAdapter;
132     }
133  
134     private void ensureList() {
135         if (mList != null) {
136             return;
137         }
138         setContentView(com.android.internal.R.layout.list_content_simple);
139  
140     }
141  
142     private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
143         public void onItemClick(AdapterView<?> parent, View v, int position, long id)
144         {
145             onListItemClick((ListView)parent, v, position, id);
146         }
147     };
148 }

代码如上,在使用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)