ListActivity 应用之多层列表

ListActivity 的源代码:

ListActivity
public class ListActivity extends Activity {

protected ListAdapter mAdapter;
protected ListView mList;

private Handler mHandler = new Handler();
private boolean mFinishedStart = false;

private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};

protected void onListItemClick(ListView l, View v, int position, long id) {
}
@Override
protected void onRestoreInstanceState(Bundle state) {
ensureList();
super.onRestoreInstanceState(state);
}
@Override
protected void onDestroy() {
mHandler.removeCallbacks(mRequestFocus);
super.onDestroy();
}

@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;
}
public void setListAdapter(ListAdapter adapter) {
synchronized (this) {
ensureList();
mAdapter = adapter;
mList.setAdapter(adapter);
}
}
public void setSelection(int position) {
mList.setSelection(position);
}
public int getSelectedItemPosition() {
return mList.getSelectedItemPosition();
}
public long getSelectedItemId() {
return mList.getSelectedItemId();
}
public ListView getListView() {
ensureList();
return mList;
}

public ListAdapter getListAdapter() {
return mAdapter;
}

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

}
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 的源代码中我们可以看到其本省含有一个ListView 和一个ListAdapter 成员变量,ListActivity容纳的ListView对象,能够被绑定到不同的数据源,一般是一个数组或者存储了一组查询结果的游标。

ListActivity的默认布局由一个位于屏幕中心的全屏列表构成。但是,如果你不想使用默认的布局,可以在onCreate()方法中通过setContentView()方法设定你自己定制的布局。

"Your content must have a ListView whose id attribute is android.R.id.list"

如果指定你自己定制的布局,你的布局中必须包含一个id为"@android:id/list"(如果是使用代码的形式,则是 list)的ListView 。此外,你自定义的view为空时,能够包含另外一个任何类型的view对象。

"empty list"notifier必须有一个id"android:empty"。要注意的是:Note that when an empty view is present, the list view will be hidden when there is no data to display.

XML
 <?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">

<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>

<TextView id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>

自己写的一个demon


下载地址:http://download.csdn.net/detail/xtlvice/3767088


posted on 2011-11-07 21:01  xtl  阅读(695)  评论(0编辑  收藏  举报

导航