数据源,group为组数据,tableName为子项数据:
List<Map<String, String>> tableNameList = new ArrayList<>();
Map<String, String> map = new HashMap<>();
map.put("tableName", tablename.title());
map.put("group", tablename.group());
map.put("actClass", tablename.actClass());
tableNameList.add(map);
可以直接调用此方法实现,使用系统自带适配器:
private void initSurveyType(final ExpandableListView lvTypeList, final List<Map<String, String>> tableNameList) {
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, initGroupData(tableNameList),
R.layout.bottom_survey_group_list_item, new String[]{"group"}, new int[]{R.id.survey_group_item},
initChildData(tableNameList), R.layout.bottom_survey_child_list_item, new String[]{"tableName"}, new int[]{R.id.survey_child_item});
lvTypeList.setAdapter(adapter);
// 设置分组项的点击监听事件
lvTypeList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// 请务必返回 false,否则分组不会展开
return false;
}
});
// 设置子选项点击监听事件
lvTypeList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
HashMap<String, String> map = (HashMap<String, String>) initChildData(tableNameList).get(groupPosition).get(childPosition);
String actClass = map.get("actClass");
String tableName = map.get("tableName");
try {
Class toActClass = Class.forName("com.mapgis.www.geosurvey.sheets." + actClass);//表单类名绝对路径
Intent intent = new Intent(BaseDataSheetActivity.this, toActClass).putExtra("tableName", tableName);
startActivityForResult(intent, REQUEST_CODE_SHEET_ACTIVITY);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return true;
}
});
}
// 构建分组项显示的数据
private List<Map<String, String>> initGroupData(List<Map<String, String>> tableNameList) {
List<Map<String, String>> list = new ArrayList<>();
for (int i = 0; i < tableNameList.size(); i++) {
Map<String, String> maps = tableNameList.get(i);
Map<String, String> map = new HashMap<>();
map.put("group", maps.get("group"));
list.add(map);
}
//去重
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i).equals(list.get(j))) {
list.remove(j);
--j;
}
}
}
return list;
}
// 构建子选项显示的数据
private List<List<Map<String, String>>> initChildData(List<Map<String, String>> tableNameList) {
List<List<Map<String, String>>> list = new ArrayList<>();
List<Map<String, String>> child;
List<Map<String, String>> group = initGroupData(tableNameList);
for (int j = 0; j < group.size(); j++) {
child = new ArrayList<>();
for (int i = 0; i < tableNameList.size(); i++) {
Map<String, String> maps = tableNameList.get(i);
if (tableNameList.get(i).get("group").equals(group.get(j).get("group"))) {
child.add(maps);
}
}
list.add(child);
}
return list;
}