上一篇:http://www.bkjia.com/kf/201208/146527.html
Expandable List 與普通List的區別在於Expandable List可以顯示兩個層次,第一層稱為”Group” 第二層為”Child” 。其中”Child”可以摺疊或展開,有點像只支援兩級的TreeView(檔案瀏覽器)。
用法上和List 非常類似,下面的對應關係可以協助理解Expandable List
ExpandableListActivity –>ListActivity
ExpandableListView –> ListView
ExpandableListAdapter –>ListAdapter
ExpandableListActivity 預設的Layout 為一置中全螢幕顯示的列表(兩級)。Android也允許使用自訂的Layout來顯示Expandable List. 為了使用自訂的Layout的,在XML中必須包括一個ExpandableListView對象且id必須為 @android:id/list。 此外也可以為空白列表定義一個View,此View的id為 @id/android:empty。
如下例為ExpandableListActivity 自訂一個Layout ,為在列表為空白時顯示“No Data”.可以在onCreate 使用setContentView設定這個自訂Layout .
<?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”>
<ExpandableListView 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 android:id=”@id/android:empty”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#FF0000″
android:text=”No data”/>
< /LinearLayout>
ExpandableListActivity 調用setListAdapter(ExpandableListAdapter) 設定ExpandableListAdapter, ExpandableListAdapter 允許為”group” 和 “child”分別設定View。
例如本例 MyExpandableListAdapter 派生自BaseExpandableListAdapter,重載了getChildView 和getGroupView,分別為group 和child定義一個TextView,TextView的文本為Group名稱或是Child的名稱,分別對應在groups 和children。
[java]
// Sample data set. children[i] contains
//the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names",
"Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
// Sample data set. children[i] contains
//the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names",
"Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
getExpandableListView() 取得當前Activity 對應的ExpandableListView, 然後調用registerForContextMenu 為 ExpandableListView 添加Context Menu.
然後在onContextItemSelected事件中為List View 中的每個list Item 添加事件,本例使用Toast 顯示一行文本,顯示當前點擊的事件。
[java]
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info
= (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos
= ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos
+ " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos
+ " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info
= (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos
= ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos
+ " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos
+ " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
作者:mapdigit