0

我想在查看组项目时展开它,但由于某些原因,我无法在展开后折叠组项目。

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

    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

我注意到当我只点击 group0 时它显示 groupId0 & 4

编辑:重新编写以使其更清楚,以便其他人更好地理解

4

2 回答 2

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) {

两者都有最终的 int groupPosition。这就是为什么 expandablelistview 不知道我要折叠哪个 groupPosition 的原因。此外,因此,当我仅单击 group0 时,它会打印出 groupId0 和 4。

ExpandalblelistView 对我来说很难处理 ^$^**。我希望这对其他人有帮助。

于 2014-08-20T11:50:14.580 回答
0

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

    lv.setOnGroupExpandListener(new OnGroupExpandListener() {

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

});

如果仍然有任何问题,请联系我如果没有任何问题,然后添加视图和视图组

于 2014-08-20T04:27:14.693 回答