先看效果,過瘾一番。
源碼下載下傳:http://files.cnblogs.com/salam/WidgetDemo.rar
ExpandableListView是Android中的手風琴,本人感覺效果相當棒。
一、ExpandableListView介紹
一個垂直滾動的顯示兩個級别(Child,Group)清單項的視圖,清單項來自ExpandableListAdapter 。組可以單獨展開。
1.重要方法
expandGroup(int groupPos) :在分組清單視圖中展開一組,
setSelectedGroup(int groupPosition) :設定選擇指定的組。
setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :設定選擇指定的子項。
getPackedPositionGroup(long packedPosition) :傳回所選擇的組
getPackedPositionForChild(int groupPosition, int childPosition) :傳回所選擇的子項
getPackedPositionType(long packedPosition) :傳回所選擇項的類型(Child,Group)
isGroupExpanded(int groupPosition) :判斷此組是否展開
2.代碼:
ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
String title=((TextView)menuInfo.targetView).getText().toString();
int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);
二、ExpandableListAdapter
一個接口,将基礎資料連結到一個ExpandableListView。此接口的實施将提供通路Child的資料(由組分類),并執行個體化的Child和Group。
getChildId(int groupPosition, int childPosition) 擷取與在給定組給予孩子相關的資料。
getChildrenCount(int groupPosition) 傳回在指定Group的Child數目。
2.代碼
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
public String[] groups = { "我的好友", "新疆同學", "親戚", "同僚" };
public String[][] children = {
{ "胡算林", "張俊峰", "王志軍", "二人" },
{ "李秀婷", "蔡喬", "别高", "餘音" },
{ "攤派新", "張愛明" },
{ "馬超", "司道光" }
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableListDemo.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
public Object getGroup(int groupPosition) {
return groups[groupPosition];
public int getGroupCount() {
return groups.length;
public long getGroupId(int groupPosition) {
return groupPosition;
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
textView.setText(getGroup(groupPosition).toString());
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
public boolean hasStableIds() {