setOnCheckedChangeListener

时间:2014-08-18 04:03:37

标签: android-listview expandablelistview android-checkbox oncheckedchanged

我想在检查时展开群组项目,但由于某些原因,我不能在群组项目展开后将其折叠。

有人可以告诉我我做错了吗?

    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    final GroupHolder holder;
    final ViewGroup viewGroup = parent;

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.group_list, null);
        holder = new GroupHolder();
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.cb);
        holder.imageView = (ImageView) convertView.findViewById(R.id.label_indicator);
        holder.title = (TextView) convertView.findViewById(R.id.group_title);
        convertView.setTag(holder);
    } else {
        holder = (GroupHolder) convertView.getTag();
    }

    holder.imageView.setImageResource(groupStatus[groupPosition] == 0 ? R.drawable.group_down: R.drawable.group_up);
    final Item groupItem = getGroup(groupPosition);

    holder.title.setText(groupItem.name);

    holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!ALL_CHECKED) {

                ArrayList<Item> childItem = getChild(groupItem);                                        
                for (Item children : childItem)
                    children.isChecked = isChecked;
                }               

            groupItem.isChecked = isChecked;
            // TODO Auto-generated method stub
            if(groupItem.isChecked)
            ((ExpandableListView) viewGroup).expandGroup(groupPosition);

            notifyDataSetChanged();
            new Handler().postDelayed(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub
                    if (ALL_CHECKED)
                        ALL_CHECKED = false;
                }
            }, 10);

        }

    });
    holder.checkbox.setChecked(groupItem.isChecked);
    return convertView;
  }

//下面是我只在group0上添加复选标记时的logcat:

08-19 07:49:08.860: D/group pos: 0
08-19 07:49:08.863: D/group pos: 0
08-19 07:49:08.887: D/group pos: 4

我注意到它显示了groupId0&amp; 4当我仅点击group0

编辑:重新编写,以便让其他人更清楚地了解

2 个答案:

答案 0 :(得分:0)

我有一个想法。  首先删除CheckBOx并尝试通过此

进行折叠
    lv.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {
        if (lastExpandedPosition != -1
                && groupPosition != lastExpandedPosition) {
            lv.collapseGroup(lastExpandedPosition);
        }
        lastExpandedPosition = groupPosition;
}

});

如果仍有任何问题,请在没有任何问题的情况下ping我,然后添加视图和查看群组

答案 1 :(得分:0)

首先,非常感谢Blue Green寻求帮助。我经过几天的调试后想出来了。它没有让我崩溃的原因是

public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

AND

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

BOTH有最终的int groupPosition。这就是expandablelistview不知道我想要崩溃的groupPosition的原因。此外,正因为如此,它打印出groupId0&amp; 4当我只点击group0时。

ExpandalblelistView非常^ ^ ^ **很难为我处理。我希望这有助于其他人。