关于伸缩列表的ExpandableListView+CheckBox

       最近在做关于伸缩列表这块功能,在网上找了许多关于ExpandableListView+CheckBox的例子,不是代码比较臃肿,就是写法混乱,都没什么参考意义,所以动手写了个简洁易懂的Demo.

           思路:一、自定义一个adapter,继承BaseExpandableListAdapter,在该类中创建两个列表键值对存储对象,分别将位置id和boolean值存进去,在getGroupView和getChildView中调用相应的方法进行判断

                  二、同时也可在该类下创建接口,实现选中数据、个数回传。思路简单易理解。

源码介绍:

1、将GroupList和ChildList传入

private List<Map<Integer, Boolean>> gpList = new ArrayList<>();
    private List<List<Map<Integer, Boolean>>> cdList = new ArrayList<>();

    public <T> void setItemCheck(List<T> list1, List<List<T>> list2) {
        gpList = new ArrayList<>();
        cdList = new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            Map<Integer, Boolean> m = new HashMap<>();
            m.put(i, false);
            gpList.add(i, m);
            Map<Integer, Boolean> map = new HashMap<>();
            List<Map<Integer, Boolean>> list = new ArrayList<>();
            for (int j = 0; j < list2.get(i).size(); j++) {
                map.put(j, false);
                list.add(j, map);
            }
            cdList.add(i, list);
        }
    }

2、全选和全不选  只需要将上面的的false改为true即可,同时要记得调用notifyDataSetChanged

 public <T> void unCheckAll(List<T> list1, List<List<T>> list2) {
        setItemCheck(list1, list2);
        this.notifyDataSetChanged();
    }

    public <T> void checkAll(List<T> list1, List<List<T>> list2) {
        gpList = new ArrayList<>();
        cdList = new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            Map<Integer, Boolean> m = new HashMap<>();
            m.put(i, true);
            gpList.add(i, m);
            Map<Integer, Boolean> map = new HashMap<>();
            List<Map<Integer, Boolean>> list = new ArrayList<>();
            for (int j = 0; j < list2.get(i).size(); j++) {
                map.put(j, true);
                list.add(j, map);
            }
            cdList.add(i, list);
        }
        this.notifyDataSetChanged();
    }

3、在onChildClick()方法中调用改类下toggleChild方法

   public void toggleChild(int groupPos, int childPos) {
        if (isShowChk()) {
            if (cdList.get(groupPos).get(childPos).get(childPos)) {
                cdList.get(groupPos).get(childPos).put(childPos, false);
            } else {
                cdList.get(groupPos).get(childPos).put(childPos, true);
            }
            if (cdList.get(groupPos).get(childPos).containsValue(false)) {
                gpList.get(groupPos).put(groupPos, false);
            } else {
                gpList.get(groupPos).put(groupPos, true);
            }
            this.notifyDataSetChanged();
        }
    }

3、在getChildView和geiGroupView中调用setChildChkView()和setGroupChkView()方法

注意:1、 checkBox.setFocusable(false);让checkbox失去焦点,否则onChildClick()和onGroupClick()方法不会被调用,造成不可点击的问题
      2、 checkBox.setClickable(false); 让checkbox不可点击,这里要注意,为了不与onChildClick()点击事件冲突

 private void toggleGroup(int groupPos) {
        if (isShowChk()) {
            if (gpList.get(groupPos).get(groupPos)) {
                gpList.get(groupPos).put(groupPos, false);
            } else {
                gpList.get(groupPos).put(groupPos, true);
            }
            if (gpList.get(groupPos).get(groupPos)) {
                for (int i = 0; i < cdList.get(groupPos).size(); i++) {
                    cdList.get(groupPos).get(i).put(i, true);
                }
            } else {
                for (int i = 0; i < cdList.get(groupPos).size(); i++) {
                    cdList.get(groupPos).get(i).put(i, false);
                }
            }
            this.notifyDataSetChanged();
        }
    }

    protected void setChildChkView(int groupPos, int childPos, CheckBox checkBox) {
        if (isShowChk()) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setFocusable(false);
            checkBox.setClickable(false);
            if (cdList.get(groupPos).get(childPos).get(childPos)) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
        } else {
            checkBox.setVisibility(View.GONE);
        }
    }

    protected void setGroupChkView(final int groupPos, CheckBox checkBox) {
        if (isShowChk()) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setFocusable(false);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toggleGroup(groupPos);
                    callBack.setCheckedNum(getCheckedNum());
                }
            });
            if (gpList.get(groupPos).get(groupPos)) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
        } else {
            checkBox.setVisibility(View.GONE);
        }
    }


 
 

此实现方式不会照成过多的代码,核心逻辑易写完,易读性强,可修改性强。

        

源码下载地址


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用ExpandableListView可以实现QQ好友分组列表。首先需要创建一个ExpandableListView控件,并为其设置适配器。适配器需要继承BaseExpandableListAdapter,并实现以下方法: 1. getGroupCount():获取分组的数量。 2. getChildrenCount():获取某个分组下子项的数量。 3. getGroup():获取某个分组的数据。 4. getChild():获取某个分组下某个子项的数据。 5. getGroupId():获取某个分组的ID。 6. getChildId():获取某个分组下某个子项的ID。 7. hasStableIds():判断分组和子项的ID是否稳定。 8. getGroupView():获取分组的视图。 9. getChildView():获取子项的视图。 10. isChildSelectable():判断子项是否可选中。 在实现适配器的过程中,需要根据数据源的结构来设置分组和子项的数据。例如,可以使用一个List<List<String>>来存储分组和子项的数据,其中外层List表示分组,内层List表示子项。在getGroup()和getChild()方法中,需要根据groupPosition和childPosition来获取对应的数据。 最后,需要为ExpandableListView设置分组的展开和收起事件。可以通过设置OnGroupClickListener和OnChildClickListener来实现。在OnGroupClickListener中,需要根据groupPosition来判断当前分组是否已经展开,如果已经展开则返回false,否则返回true。在OnChildClickListener中,可以根据childPosition来获取对应的数据,并进行相应的操作。 通过以上步骤,就可以实现一个简单的QQ好友分组列表

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番茄小能手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值