KHExpandableListAdapter.java
public class KHExpandableListAdapter extends BaseExpandableListAdapter {
private List<Map<String, Object>> groups;
private List<List<Map<String, Object>>> childs;
private Context context;
public KHExpandableListAdapter(Context context,List<Map<String, Object>> groups, List<List<Map<String, Object>>> childs){
this.context = context;
this.groups = groups;
this.childs = childs;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView childView = getGenericView();
childView.setText(childs.get(groupPosition).get(childPosition).get("child").toString());
return childView;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView groupView = getGenericView();
groupView.setText(groups.get(groupPosition).get("group").toString());
return groupView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private TextView getGenericView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 36);
TextView textView = new TextView(context);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
return textView;
}
}
ExpandableListViewActivity.java
public class ExpandableListViewActivity extends Activity {
private ExpandableListView ELV;
private List<Map<String,Object>> groups;
private List<List<Map<String,Object>>> childs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ELV = new ExpandableListView(this);
ELV.setBackgroundColor(Color.BLUE);
groups = new ArrayList<Map<String,Object>>();
childs = new ArrayList<List<Map<String,Object>>>();
List<Map<String,Object>> child1 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> child2 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> child3 = new ArrayList<Map<String,Object>>();
List<Map<String,Object>> child4 = new ArrayList<Map<String,Object>>();
for(int i = 0; i < 4; i++){
Map<String,Object> group = new HashMap<String,Object>();
group.put("group", "第 " + i + " 组");
groups.add(group);
}
for(int i = 0; i < 5; i++){
Map<String,Object> child = new HashMap<String,Object>();
child.put("child", "第 " + i + " 个child");
child1.add(child);
child2.add(child);
child3.add(child);
child4.add(child);
}
childs.add(child1);
childs.add(child2);
childs.add(child3);
childs.add(child4);
KHExpandableListAdapter KHA = new KHExpandableListAdapter(this,groups,childs);
ELV.setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Toast.makeText(ExpandableListViewActivity.this, childs.get(groupPosition).get(childPosition).get("child").toString(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("wf" , groups.get(groupPosition).get("group").toString());
intent.putExtra("user" , childs.get(groupPosition).get(childPosition).get("child").toString());
ExpandableListViewActivity.this.setResult(RESULT_OK, intent);
ExpandableListViewActivity.this.finish();
return false;
}
});
ELV.setAdapter(KHA);
this.setContentView(ELV);
}
}
<activity android:name=".ExpandableListViewActivity" android:theme="@android:style/Theme.Dialog" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.ELV" /> </intent-filter> </activity>