创建适配器 实现4个方法:getGroupCount()/ getGroupView() getChildCount()/ getChildView()

public class MyAdapter extends BaseExpandableListAdapter {

private ArrayList<String> mGroupNames;
private HashMap<String, ArrayList<String>> mChildNames;

public MyAdapter(ArrayList<String> groupNames, HashMap<String, ArrayList<String>> childNames){
mGroupNames=groupNames;
mChildNames=childNames;
}

@Override
public int getGroupCount() {
return mGroupNames.size();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(android.R.layout.simple_list_item_1, null);
TextView tv = (TextView) itemView.findViewById(android.R.id.text1); tv.setText(mGroupNames.get(groupPosition));
return itemView;
}

@Override
public int getChildrenCount(int groupPosition) { //1.获取组
String groupName = mGroupNames.get(groupPosition); //2.根据组名获取⼦子列表
ArrayList<String> childNames = mChildNames.get(groupName);
return childNames.size();
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(android.R.layout.simple_list_item_1, null);

//1.控件已经被获取了
TextView tv = (TextView) itemView.findViewById(android.R.id.text1);

//2.获取⼦子视图的数据

//3.获取组
String groupName = mGroupNames.get(groupPosition);

//4.根据组名获取⼦子列表
ArrayList<String> childNames = mChildNames.get(groupName);

//5.从⼦子列表⾥里⾯面获取某个Item
String text = childNames.get(childPosition);

tv.setText(text);
return itemView;

}

}

 

1.在Activity的onCreate中创建数据 并将数据绑定到列表中

2.设置列表监听器 (组折叠/展开/点击监听器 子视图点击的监听器)

public class MainActivity extends Activity {
private ExpandableListView mExpLv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mExpLv=(ExpandableListView) findViewById(R.id.explv);
//1.组的数据
ArrayList<String> groupNames=new ArrayList<String>();
for (int i = 0; i < 3; i++) {
groupNames.add("组 "+i);
}
//2.成员的数据
HashMap<String, ArrayList<String>> childNames=new HashMap<String, ArrayList<String>>();
for (int i = 0; i < groupNames.size(); i++) {
ArrayList<String> children=new ArrayList<String>();
for (int j = 0; j < 5; j++) {
children.add("成员"+j);
}
//遍历下组名 key就是组名 值就时⼀一个成员队列
childNames.put(groupNames.get(i), children);
}

MyAdapter adapter=new MyAdapter(groupNames,childNames);
mExpLv.setAdapter(adapter);
//为ExpandableListView的组设置折叠监听器
// mExpLv.setOnGroupCollapseListener(onGroupCollapseListener)
// mExpLv.setOnGroupExpandListener(onGroupExpandListener)
// mExpLv.setOnGroupClickListener(onGroupClickListener)
// mExpLv.setOnChildClickListener(onChildClickListener)
mExpLv.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this, groupPosition+" 被展开了", 0).show();
}
});
}
}