Android之可收縮展開列表ExpandableList
在Android的app包中,有這么一個類,這個類繼承自Activity,它叫ExpandableListActivity。顧名思義,從它的名字可以看出該類是一種可擴展性的列表List,我們這里理解成可伸縮的列表,也就是通過繼承ExpandableListActivity 可以實現(xiàn)列表的可展開/收縮的功能。
本文我們主要介紹這種列表的顯示是如何實現(xiàn)的,在ListActivity的使用中,我們知道一旦繼承了ListActivity,該類就意味這具備了List的功能,同樣的,我們將一個類繼承了ExpandableListActivity,就可以直接調(diào)用該類本身的ExpandableList對象,并直接賦予一個自定義的適配器setListAdapter(adapter);,因此,整個實現(xiàn)的重心放在如何設(shè)計這個適配器上面,以下是適配器的一個舉例。
public class mExpandableListAdapter extends BaseExpandableListAdapter {
// 父列表數(shù)據(jù)
private String[] groups =
{
“隨時隨地”,
“即興時代”,
“ATAAW.COM”,
};
// 子列表數(shù)據(jù)
private String[][] children =
{
{ “即興” },
{ “隨時隨地”, “即興時代” },
{ “隨時隨地”, “即興時代”, “ATAAW.COM” },
};
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
// 取子列表中的某一項的 View
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//父列表中的某一項的View
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
//獲取某一項的View
private TextView getGenericView() {
TextView textView = new TextView(_ExpandableList.this);
return textView;
}
}
可以看出,在實現(xiàn)可伸縮列表上,我們需要集中精神把重頭戲放在這個適配器上面。文章地址
?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調(diào)華麗/簡單生活/完美人生
posted on 2010-10-16 10:11 poetguo 閱讀(3733) 評論(0) 編輯 收藏 所屬分類: Android