ExpandableListView:错groupPosition返回如果有一个扩展childView [英] ExpandableListView: wrong groupPosition returned if there's an expanded childView

查看:380
本文介绍了ExpandableListView:错groupPosition返回如果有一个扩展childView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ExpandableListView;列表的单一项目(组项)具有一个包含切换按钮一个的FrameLayout。我为了增加按钮的触摸区域这样做。我使用按钮来展开它所属的组项目的子视图。结果
不能有两个在同一时间检查按钮,因为当一个人被选中,$ P $被检查pvious一个得到选中。结果
现在,我第一次点击一个项目的按钮,就会切换正常的;时,在那之后,我点击另一个项目之一,即切换按钮是一个在位置+1按钮,点击的位置。结果
但是,如果我点击了最后一个项目的按钮,这将让切换按钮是一个位于位置目前切换按钮的位置+1(这意味着如果我第一次切换1项,然后单击最后一个项目的按钮2项将切换;再次点击最后一个将切换项目3,那么第4项,第5项,依此类推直到最后,最后一个项目将切换)

I have an ExpandableListView; the single item (group item) of the list has a FrameLayout that contains a ToggleButton. I did this in order to increase the button's touch area. I use the button to expand the child view of the group item it belongs to.
There can't be two checked buttons at a time, because when one gets checked, the previous one that was checked gets unchecked.
Now, the first time I click on the button of an item, it toggles alright; when, after that, I click on the one of another item, the button that toggles is the one at position "position of clicked button" +1.
If, however, I clicked on the last item's button, the button that will get toggled is the one at position "position of currently toggled button" +1 (this means that if I first toggle item 1, then click on the last item's button, item 2 will toggle; clicking again on the last one will toggle item 3, then item 4, item 5 and so on until, finally, the last item will toggle).

这里的适配器的code:

here's the adapter's code:

public class CustomList extends BaseExpandableListAdapter {

private final Activity context;
public List<Item> names;//Item is a custom object
public boolean[] buttons;
public int lastChecked;
private final int imageId = R.drawable.item1;
private ExpandableListView list;

public CustomList(Activity context, List<Item> names, ExpandableListView theListView) {

    this.context = context;
    this.names = names;
    this.buttons = new boolean[names.size()];
    this.lastChecked = -1;
    this.list = theListView;
}

private static class GroupHolder {

    TextView txtTitle;
    TextView txtData;
    ImageView imageView;
    ToggleButton toggle;
    FrameLayout frame;
}

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

    GroupHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_single, null);

        holder = new GroupHolder();

        holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
        holder.txtData = (TextView) convertView.findViewById(R.id.numberOfFiles);
        holder.imageView = (ImageView) convertView.findViewById(R.id.img);
        holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
        holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);

        holder.frame.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_UP) {

                    if(lastChecked != -1) {//if there's a checked button
                        buttons[lastChecked] = !buttons[lastChecked];//uncheck it

                    if(position == lastChecked)//if I clicked on the last checked button 
                        lastChecked = -1;//there's no checked button
                    else {
                        buttons[position] = !buttons[position];//check the button
                        lastChecked = position;//and set it as the last checked one
                    }

                    notifyList();
                }

                return false;
            }           
        });

        convertView.setTag(holder);

    } else {

        holder = (GroupHolder) convertView.getTag();
    }

    holder.txtTitle.setText(names.get(position).getName());

    holder.txtData.setText(names.get(position).getData());

    holder.imageView.setImageResource(imageId);

    holder.toggle.setChecked(buttons[position]);

    if(holder.toggle.isChecked()) {

        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
            list.expandGroup(position);
        else
            list.expandGroup(position, true);
    } else {

        list.collapseGroup(position);
    }

    return convertView;
}

private static class ChildHolder {

    TextView details;
    TextView send;
    TextView other;
}

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

    ChildHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_child, null);

        holder = new ChildHolder();

        holder.details = (TextView) convertView.findViewById(R.id.details);
        holder.send = (TextView) convertView.findViewById(R.id.send);
        holder.other = (TextView) convertView.findViewById(R.id.other);

        convertView.setTag(holder);

    } else {

        holder = (ChildHolder) convertView.getTag();
    }

    holder.details.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
            lastChecked = -1;//there's no checked button

            notifyList();
            //*call some method in the main activity*
        }
    });

    holder.send.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
            lastChecked = -1;//there's no checked button

            notifyList();
            //*call some method in the main activity*
        }
    });

    holder.other.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
            lastChecked = -1;//there's no checked button

            notifyList();
            //*call some method in the main activity*
        }
    });

    return convertView;
}

public void notifyList() {

    this.notifyDataSetChanged();
}

正如你所看到的,在getGroupView,一个孩子得到的项目取决于项目的对应于其在列表视图的位置布尔数组中的值,展开或折叠。我叫notifyDataSetChanged()来更新列表。结果
在主要活动中,我设置了onGroupClickListener到ExpandableListView并在onGroupClick为了不扩大/对本集团的项目,当点击折叠子视图(因为我用的ToggleButtons本)返回true。

As you can see, in the getGroupView, a child item gets expanded or collapsed depending on the value of the item in the boolean array that corresponds to the position of its view in the list. I call notifyDataSetChanged() to update the list.
in the main activity, I set the onGroupClickListener to the ExpandableListView and return true in the onGroupClick in order to not expand/collapse the child views when clicking on the group items (since I use the ToggleButtons for this).

推荐答案

好吧,这是一个非常蹩脚的错误。我不得不每getGroupView被称为时间,而不是仅在第一次设置OnTouchListener为的FrameLayout。所以该方法的右code是这样的:

Ok, it was a very lame error. I had to set the OnTouchListener for the FrameLayout every time getGroupView was called, not only the first time. So the right code of the method is this:

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

    GroupHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_single, null);

        holder = new GroupHolder();

        holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
        holder.txtData = (TextView) convertView.findViewById(R.id.numberOfFiles);
        holder.imageView = (ImageView) convertView.findViewById(R.id.img);
        holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
        holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);

        convertView.setTag(holder);

    } else {

        holder = (GroupHolder) convertView.getTag();
    }

    holder.txtTitle.setText(names.get(position).getName());

    holder.txtData.setText(names.get(position).getData());

    holder.imageView.setImageResource(imageId);

    holder.frame.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if(event.getAction() == MotionEvent.ACTION_UP) {

                if(lastChecked != -1) {//if there's a checked button
                    buttons[lastChecked] = !buttons[lastChecked];//uncheck it

                if(position == lastChecked)//if I clicked on the last checked button 
                    lastChecked = -1;//there's no checked button
                else {
                    buttons[position] = !buttons[position];//check the button
                    lastChecked = position;//and set it as the last checked one
                }

                notifyList();
            }

            return false;
        }
    });

    holder.toggle.setChecked(buttons[position]);

    if(holder.toggle.isChecked()) {

        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
            list.expandGroup(position);
        else
            list.expandGroup(position, true);
    } else {

        list.collapseGroup(position);
    }

    return convertView;
}

这篇关于ExpandableListView:错groupPosition返回如果有一个扩展childView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆