Skip to content Skip to sidebar Skip to footer

Expandablelistview: Wrong Groupposition Returned If There's An Expanded Childview

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 us

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

Post a Comment for "Expandablelistview: Wrong Groupposition Returned If There's An Expanded Childview"