jmov1
jmov1
2015-12-30 06:22
采纳率: 100%
浏览 3.9k

android ExpandableListView+CheckBox实现组选列表

需要用ExpandableListView+CheckBox实现一个组选列表,ExpandableListView组列表为部门,子列表为成员。需要选中部门的CheckBox时,选中所有组内成员CheckBox,取消时全部取消。选择单个组内成员时,未全部选中该部门成员,则部门CheckBox不勾选,如选中了所有成员,则自动勾选部门CheckBox。希望以前做过的朋友可以给个详细的例子

下面附上我的代码


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;



import java.util.ArrayList;
import java.util.List;
import java.util.Map;



public class ReportListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<DepartmentEntity> parentList;
    private Map<String, List<EmployeeEntity>> map;

    private List<Boolean> parentStatusList = new ArrayList<Boolean>();
    private List<Boolean> childStatusList = new ArrayList<Boolean>();

    public ReportListAdapter(Context context, List<DepartmentEntity> parentList,
                             Map<String, List<EmployeeEntity>> map) {
        this.context = context;
        this.parentList = parentList;
        this.map = map;

    }

    // 得到子item需要关联的数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = parentList.get(groupPosition).getId();
        return (map.get(key).get(childPosition));
    }

    // 得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 设置子item的组件
    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             final boolean isLastChild, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.report_list_child_item,
                    null);
        }

        String key = this.parentList.get(groupPosition).getId();
        final EmployeeEntity employee = map.get(key).get(childPosition);
        String name = employee.getName();
        TextView employeeName = BaseViewHolder.get(convertView,
                R.id.employee_name);
        employeeName.setText(name);

        CheckBox childCB = BaseViewHolder.get(convertView, R.id.employee_checkbox);
//            if (employee.isChecked()) {
//                cb.setChecked(true);
//            } else {
//                cb.setChecked(false);
//            }
        childCB.setChecked(employee.isChecked());
//        notifyDataSetChanged();
        childCB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CheckBox nowCB = (CheckBox) view;
                if (nowCB.isChecked()) {
                    employee.setIsChecked(true);
//
//                    //获得子item数量
                    int childSize = getChildrenCount(groupPosition);
                    //其他子item状态
                    List<Boolean> otherChildStatus = new ArrayList<Boolean>();
                    otherChildStatus.clear();
                    for (int i = 0; i < childSize; i++) {
                        if (i != childPosition) {
                            boolean isLastChild1;
                            if (i == childSize - 1) {
                                isLastChild1 = true;
                            } else {
                                isLastChild1 = false;
                            }
                            //获得子item
                            View view1 = getChildView(groupPosition, i, isLastChild1, null, null);
                            CheckBox childCB = (CheckBox) view1.findViewById(R.id.employee_checkbox);
                            boolean cbStatus;
                            if (childCB.isChecked()) {
                                cbStatus = true;
                            } else {
                                cbStatus = false;
                            }
                            otherChildStatus.add(cbStatus);
                        }
                    }

                    //获得父item
                    View parentView1 = getGroupView(groupPosition, true, null, null);
                    //获取父item的cb
                    CheckBox parentCB = (CheckBox) parentView1.findViewById(R.id.department_checkbox);

                    //设置父Item选项框状态
                    for (int i = 0; i < otherChildStatus.size(); i++) {

                        if (!otherChildStatus.get(i)) {
                            parentList.get(groupPosition).setIsChecked(false);
                            parentCB.setChecked(false);
                            System.out.println("设置父cb不选");

                            break;
                        } else {
                            parentList.get(groupPosition).setIsChecked(true);
                            parentCB.setChecked(true);
                            notifyDataSetChanged();

                            System.out.println("设置父cb全选中");

                        }
                    }
                    notifyDataSetChanged();
                } else {
                    //获得父item
                    View parentView = getGroupView(groupPosition, true, null, null);


                    employee.setIsChecked(false);
                    CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox);
                    parentList.get(groupPosition).setIsChecked(false);
                    parentCB.setChecked(false);

                    System.out.println("设置父cb不选");

                    notifyDataSetChanged();
                }
            }
        });


//        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//                //获得父item
//                View parentView = getGroupView(groupPosition, true, null, null);
//                if (isChecked) {
//                    employee.setIsChecked(true);
//
//                    //获得子item数量
//                    int childSize = getChildrenCount(groupPosition);
//                    //其他子item状态
//                    List<Boolean> otherChildStatus = new ArrayList<Boolean>();
//                    for (int i = 0; i < childSize; i++) {
//                        if (i != childPosition) {
//                            boolean isLastChild1;
//                            if (i == childSize - 1) {
//                                isLastChild1 = true;
//                            } else {
//                                isLastChild1 = false;
//                            }
//                            //获得子item
//                            View view = getChildView(groupPosition, i, isLastChild1, null, null);
//                            CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox);
//                            boolean cbStatus;
//                            if (childCB.isChecked()) {
//                                cbStatus = true;
//                            } else {
//                                cbStatus = false;
//                            }
//                            otherChildStatus.add(cbStatus);
//                        }
//                    }
//                    //设置父Item选项框状态
//                    for (int i = 0; i < otherChildStatus.size(); i++) {
//                        CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox);
//                        if (otherChildStatus.get(i) == false) {
//                            parentCB.setChecked(false);
//                            parentList.get(groupPosition).setIsChecked(false);
//                            break;
//                        } else {
//                            parentCB.setChecked(true);
//                            parentList.get(groupPosition).setIsChecked(true);
//                        }
//                    }
//                    notifyDataSetChanged();
//                } else {
//                    employee.setIsChecked(false);
//                    CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox);
//                    parentCB.setChecked(false);
//                    parentList.get(groupPosition).setIsChecked(false);
//                    notifyDataSetChanged();
//                }
//
//            }
//        });


        return convertView;
    }

    // 获取当前父item下的子item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        String key = parentList.get(groupPosition).getId();
        int size = map.get(key).size();
        return size;
    }

    // 获取当前父item的数据
    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

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

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 设置父item组件
    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.report_list_parent_item,
                    null);
        }
        ImageView mgroupimage = BaseViewHolder.get(convertView, R.id.arrow);
        mgroupimage.setImageResource(R.mipmap.la);
        if (!isExpanded) {
            mgroupimage.setImageResource(R.mipmap.shou);
        }
        TextView departmentName = BaseViewHolder.get(convertView, R.id.department_name);
        departmentName.setText(parentList.get(groupPosition).getName());

        final CheckBox cb = BaseViewHolder.get(convertView, R.id.department_checkbox);


        cb.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        System.out.println("点击了父" + groupPosition);
                        CheckBox nowCB = (CheckBox) view;
                        if (nowCB.isChecked()) {
                            //获取子item的数量
                            int childSize = getChildrenCount(groupPosition);
                            //子item全选中
                            for (int i = 0; i < childSize; i++) {
                                //获取子item
                                EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
                                child.setIsChecked(true);
                                boolean isLastChild;
                                if (i == childSize - 1) {
                                    isLastChild = true;
                                } else {
                                    isLastChild = false;
                                }
                                //获得子item
                                View view1 = getChildView(groupPosition, i, isLastChild, null, null);
                                CheckBox childCB = (CheckBox) view1.findViewById(R.id.employee_checkbox);
                                childCB.setChecked(true);
                                System.out.println("子" + groupPosition + "-" + i + "  已经选中");

                            }
                            //设置父item被完全选中
                            parentList.get(groupPosition).setIsChecked(true);
                            cb.setChecked(true);
                            System.out.println("父" + groupPosition + "  已经选中");

                            notifyDataSetChanged();
                        } else {
                            //获取子item的数量
                            int childSize = getChildrenCount(groupPosition);
                            //子Item全都不选中
                            for (int i = 0; i < childSize; i++) {
                                //获取子item数据
                                EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
                                child.setIsChecked(false);
                                boolean isLastChild;
                                if (i == childSize - 1) {
                                    isLastChild = true;
                                } else {
                                    isLastChild = false;
                                }
                                //获得子item
                                View view2 = getChildView(groupPosition, i, isLastChild, null, null);
                                CheckBox childCB = (CheckBox) view2.findViewById(R.id.employee_checkbox);
                                childCB.setChecked(false);
                                System.out.println("子" + groupPosition + "-" + i + "  已经设为没选中");
                            }
                            parentList.get(groupPosition).setIsChecked(false);

                            notifyDataSetChanged();
                        }
                    }
                }

        );

        int childSize = getChildrenCount(groupPosition);
        List<Boolean> nowChildStatus = new ArrayList<Boolean>();
        nowChildStatus.clear();


//        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//                System.out.println("点击了父" + groupPosition);
//                if (isChecked) {
//                    //获取子item的数量
//                    int childSize = getChildrenCount(groupPosition);
//                    //子item全选中
//                    for (int i = 0; i < childSize; i++) {
//                        //获取子item
//                        EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
//                        child.setIsChecked(true);
//                        boolean isLastChild;
//                        if (i == childSize - 1) {
//                            isLastChild = true;
//                        } else {
//                            isLastChild = false;
//                        }
//                        //获得子item
//                        View view = getChildView(groupPosition, i, isLastChild, null, null);
//                        CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox);
//                        childCB.setChecked(true);
//                        System.out.println("子" + groupPosition + "-" + i + "  已经选中");
//                    }
//                    //设置父item被完全选中
//                    parentList.get(groupPosition).setIsChecked(true);
//                    cb.setChecked(true);
//                    System.out.println("父" + groupPosition + "  已经选中");
//
//                    notifyDataSetChanged();
//                } else {
//                    //获取子item的数量
//                    int childSize = getChildrenCount(groupPosition);
//                    //子Item全都不选中
//                    for (int i = 0; i < childSize; i++) {
//                        //获取子item数据
//                        EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
//                        child.setIsChecked(false);
//                        boolean isLastChild;
//                        if (i == childSize - 1) {
//                            isLastChild = true;
//                        } else {
//                            isLastChild = false;
//                        }
//                        //获得子item
//                        View view = getChildView(groupPosition, i, isLastChild, null, null);
//                        CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox);
//                        childCB.setChecked(false);
//                        System.out.println("子" + groupPosition + "-" + i + "  已经设为没选中");
//                    }
//                    parentList.get(groupPosition).setIsChecked(false);
//
//                    notifyDataSetChanged();
//                }
//
//            }
//        });

//        if (parentList.get(groupPosition).getIsChecked() == 2) {
//            cb.setChecked(true);
//        } else {
//            cb.setChecked(false);
//        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }


    //子集是否可选
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • u013020402
    Sevvvvv 2015-12-30 06:54
    已采纳

    http://blog.csdn.net/u013020402/article/details/50427232 父接点 groupitem事件实现一个机制 点击之后 遍历子节点的setCheck=true

    这我自己写的 单元能对你有用。

    点赞 评论
  • devmiao
    devmiao 2015-12-30 06:38
    点赞 评论

相关推荐