ExpandableListView: wrong groupPosition returned if there's an expanded childView

北慕城南 提交于 2020-02-05 14:38:47

问题


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

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

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


回答1:


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


来源:https://stackoverflow.com/questions/23569860/expandablelistview-wrong-groupposition-returned-if-theres-an-expanded-childvie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!