对同一个List类型数据实现二级ExpandableListView

数据源,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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值