最近老是和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有個這麼多認識就差不多了,請“專家”和“磚家”們批評,指正,謝謝!