1

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

我想要做的是,当用户按下按钮时,活动会找出哪些项目被“检查”并对这些项目做一些事情。

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

完毕!我现在可以跟踪已选择的内容和未选择的内容。

现在我想解决复选框不显示其状态的问题。我注意到复选框在以下情况下随机更改:

  • 屏幕旋转变化
  • 一组折叠/打开

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

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

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));
}

其中 i 和 j 指示哪个组和子项,而 checkList 跟踪已选中/未选中的项目。

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

ExpandableListView 已被使用,一定有人有办法解决这个问题。有一个聪明的解决方案吗?具体来说,我正在寻找方法来检查/取消选中 onGroupExpand 和 onConfigurationChange (或类似事件)列表中的适当项目。

先感谢您!

4

2 回答 2

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);
            }
        };

    }
于 2012-05-24T11:45:52.210 回答
0

如果您只想在单击该行时更改它:

elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        CheckBox checkbox = (CheckBox) v.findViewById(r.id.yourcheckbox)
        checkbox.setChecked(checkList.at(groupPosition, childPosition));    
        return true;
    }
});
于 2012-05-24T11:31:28.673 回答