ExpandableListView提供分組的項目清單介面,使用者可將每一組的清單內容展開或是折疊起來。
1.
首先要建立ExpandableListView所使用的版面,包括群組版面及群組的子版面。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text_college"
... />
</RelativeLayout>
child.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/image_go"
... />
<TextView
android:id="@+id/text_department"
... />
</RelativeLayout>
2.
建立ExpandableListView之資料轉接器
public class CollegeListAdapter extends
BaseExpandableListAdapter {
private Context context;
private ArrayList groups;
private ArrayList>
childs;
public CollegeListAdapter(Context
context, ArrayList groups, ArrayList>
childs) {
this.context = context;
this.groups = groups;
this.childs = childs;
}
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView,
ViewGroup parent) {
View childView;
@SuppressWarnings("unchecked")
String department = childs.get(groupPosition).get(childPosition);
LayoutInflater
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//建立子清單元件, 並設置元件對應的內容
childView =
layoutInflater.inflate(R.layout.child, null);
TextView dept_name =
(TextView) childView.findViewById(R.id.text_department);
dept_name.setText(department);
ImageView imageView =
(ImageView)childView.findViewById(R.id.image_go);
imageView.setImageResource(R.drawable.ic_go);
return childView;
}
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup
parent) {
View groupView;
String college = groups.get(groupPosition);
LayoutInflater
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//建立群組元件,並設置元件對應的內容
groupView =
layoutInflater.inflate(R.layout.group, null);
TextView college_name =
(TextView)groupView.findViewById(R.id.text_college);
college_name.setText(college);
return groupView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPos, int childPos) {
return false; //如果想要子列表可被點選,要改為回傳true
}
}
3. 建立ExpandableListView所要顯示資料串列。
ArrayList group; //群組列表
ArrayList>
child; //子列表
...
private void initializeData(){
group = new ArrayList();
child = new ArrayList>();
addInfo("電資學院",
new String[]{"電子系","電機系","資訊系","電腦通訊系"});
new String[]{"電子系","電機系","資訊系","電腦通訊系"});
addInfo("工程學院",
new String[]{"機械系","自動化系","工管系"});
new String[]{"機械系","自動化系","工管系"});
}
private void addInfo(String g,String[] c){
group.add(g);
ArrayList
childitem = new ArrayList();
for(int i=0;ilength;i++){
childitem.add(c[i]);
}
child.add(childitem);
}
4. 設定ExpandableListView資料轉接器
expandablelist = (ExpandableListView)
findViewById(R.id.expandableListView1);
expandablelist.setAdapter(
new CollegeListAdapter(MainActivity.this, group, child));
new CollegeListAdapter(MainActivity.this, group, child));