Android之ExpandableListView下拉分組的實現
ExpandableListView是android中可以實現下拉list的一個控件,具體的實現方法如下:
首先:在layout的xml文件中定義一個ExpandableListView
- <LinearLayout
- android:id="@+id/linearLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- androidrientation="vertical"
- >
- <ExpandableListView
- android:id="@+id/expandableListView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
定義兩個List,用來存放控件中Group/Child中的String
- private List<String> groupArray;
- private List<List<String>> childArray;
對這兩個List進行初始化,並插入一些數據
- groupArray = new ArrayList<String>();
- childArray = new ArrayList<List<String>>();
- groupArray.add("第一行");
- groupArray.add("第二行");
- List<String> tempArray = new ArrayList<String>();
- tempArray.add("第一條");
- tempArray.add("第二條");
- tempArray.add("第三條");
- for(int index = 0; index <groupArray.size(); ++index)
- {
- childArray.add(tempArray);
- }
定義ExpandableListView的Adapter
- //ExpandableListView的Adapter
- public class ExpandableAdapter extends BaseExpandableListAdapter
- {
- Activity activity;
- public ExpandableAdapter(Activity a)
- {
- activity = a;
- }
- public Object getChild(int groupPosition, int childPosition)
- {
- return childArray.get(groupPosition).get(childPosition);
- }
- public long getChildId(int groupPosition, int childPosition)
- {
- return childPosition;
- }
- public int getChildrenCount(int groupPosition)
- {
- return childArray.get(groupPosition).size();
- }
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent)
- {
- String string = childArray.get(groupPosition).get(childPosition);
- return getGenericView(string);
- }
- // group method stub
- public Object getGroup(int groupPosition)
- {
- return groupArray.get(groupPosition);
- }
- public int getGroupCount()
- {
- return groupArray.size();
- }
- public long getGroupId(int groupPosition)
- {
- return groupPosition;
- }
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent)
- {
- String string = groupArray.get(groupPosition);
- return getGenericView(string);
- }
- // View stub to create Group/Children 's View
- public TextView getGenericView(String string)
- {
- // Layout parameters for the ExpandableListView
- AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 64);
- TextView text = new TextView(activity);
- text.setLayoutParams(layoutParams);
- // Center the text vertically
- text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
- // Set the text starting position
- text.setPadding(36, 0, 0, 0);
- text.setText(string);
- return text;
- }
- public boolean hasStableIds()
- {
- return false;
- }
- public boolean isChildSelectable(int groupPosition, int childPosition)
- {
- return true;
- }
- }
最後,給定義好的ExpandableListView添加上Adapter
- ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
- expandableListView.setAdapter(new ExpandableAdapter(Main.this));
運行即可見效果~~~
----------------------------------------------------------------------------------------------------------------
Android版手風琴(ExpandableListView)
先看效果,過癮一番。
源碼下載:https://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。
1.重要方法
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());
return textView;
}
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 textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
最後更新:2017-04-03 12:53:57