Android:在ExpandableListView中动态标记复选框

Ji *_*Mun 1 checkbox android expandablelistview

我正在使用ExpandableListView,子行每个都有一个textview,并在其中有复选框.

我想要做的是,当用户按下按钮时,活动会确定哪些项目被"检查"并对这些项目执行某些操作.

我阅读了很多关于ExpandableListView和复选框当前不能正常工作的帖子,以及我如何保留一个额外的数据结构以跟踪已选择的内容,而不是.这篇文章涵盖了这个问题: ExpandableListView和复选框

完成!我现在可以跟踪已选择的内容以及未选择的内容.

现在我想解决复选框没有显示状态的问题.我注意到,在以下情况下,复选框会随机更改:

  • 屏幕旋转变化
  • 一个组崩溃/打开

我知道可以检测到这些事件.

我想知道是否有办法手动设置复选框.我最初尝试过

ExpandableListView elv = getExpandableListView();
for(int i = 0; i < elv.getChildCount(); i++) {
  CheckBox checkbox = ... somehow get checbox within the list
  checkbox.setChecked(checkList.at(i, j));
}
Run Code Online (Sandbox Code Playgroud)

其中i和j表示哪个组和子项,以及checkList跟踪已检查/未检查的项目.

但是,我注意到getChildCount()的值取决于打开了多少组,并不一定告诉我可以操作哪个列表项.

已经使用了ExpandableListView,并且有人必须有办法解决这个问题.这有一个聪明的解决方案吗?具体来说,我正在寻找方法来检查/取消选中onGroupExpand和onConfigurationChange(或类似事件)列表中的相应项目.

先感谢您!

Kev*_*ara 7

检查以下示例可扩展列表适配器

/**
     * A simple adapter which maintains an ArrayList of photo resource Ids. 
     * Each photo is displayed as an image. This adapter supports clearing the
     * list of photos and adding a new photo.
     *
     */
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {


        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
        private String[][] children = {
                { "Arnold", "Barry", "Chuck", "David" },
                { "Ace", "Bandit", "Cha-Cha", "Deuce" },
                { "Fluffy", "Snuggles" },
                { "Goldy", "Bubbles" }
        };

        private ArrayList<ArrayList<Boolean>> checkboxStatus = new ArrayList<ArrayList<Boolean>>();

        public MyExpandableListAdapter() {
            int groupCount = groups.length;
            for(int i=0; i<groupCount; i++) 
            {
                int childCount = children[i].length;
                ArrayList<Boolean> childStatus = new ArrayList<Boolean>();
                for(int j=0; j<childCount; j++) {
                    childStatus.add(false);
                }
                checkboxStatus.add(childStatus);
            }
        }

        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }

        public TextView getGenericView() {

            // Layout parameters for the ExpandableListView
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);

            TextView textView = new TextView(ExpandableListCheckboxActivity.this);
            textView.setLayoutParams(lp);
            // Center the text vertically
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            // Set the text starting position
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                View convertView, ViewGroup parent) {

            CheckBox chkbox = new CheckBox(ExpandableListCheckboxActivity.this);
            chkbox.setTag("" + groupPosition + "," + childPosition);
            chkbox.setOnCheckedChangeListener(checkboxListener);
            chkbox.setText(children[groupPosition][childPosition]);

            chkbox.setChecked(checkboxStatus.get(groupPosition).get(childPosition));

            return chkbox;
        }

        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }

        public int getGroupCount() {
            return groups.length;
        }

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

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());

            return textView;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public boolean hasStableIds() {
            return true;
        }

        private OnCheckedChangeListener checkboxListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String positions = buttonView.getTag().toString();
                int groupPosition = Integer.valueOf(positions.split(",")[0]);
                int childPosition = Integer.valueOf(positions.split(",")[1]);
                checkboxStatus.get(groupPosition).set(childPosition, isChecked);
            }
        };

    }
Run Code Online (Sandbox Code Playgroud)