Button to hide element in expandablelistview child

353 Views Asked by At

I have 2 buttons in my expandablelistiview child, first button View.VISIBLE, and another one View.GONE. I tried to make the first button change the second button visibility become visible, but it's not working.

My getchildview method:

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

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    buttonOff = (Button) convertView.findViewById(R.id.button_off);

    buttonOff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonOff.setVisibility(View.GONE);
            buttonTest.setVisibility(View.VISIBLE);
        }
    });

    buttonTest = (Button) convertView.findViewById(R.id.button_test);
    buttonTest.setVisibility(View.GONE);

    buttonTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonTest.setVisibility(View.GONE);
            buttonOff.setVisibility(View.VISIBLE);
        }
    });

    TextView listChildText = (TextView) convertView.findViewById(R.id.list_item);

    listChildText.setText(childText);

    return convertView;
}
1

There are 1 best solutions below

2
On

Your code is not working because you are not maintaining the state of the buttons. Initialize button state for all of your rows. Then every time your getView() method is called, first check the state and depending on that you can set which button to make visible. When a button is clicked you need to update the status of that row. Below code can guide you in that direction:

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

    final String childText = (String) getChild(groupPosition, childPosition);

    // get visibility status of the first button
    final boolean buttonOffVisible = getFirstButtonVisibility(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
    }

    buttonOff = (Button) convertView.findViewById(R.id.button_off);

    buttonOff.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // set the state of the first button
            setFirstButtonVisibility(groupPosition, childPosition, false); // false denoting as GONE

            buttonOff.setVisibility(View.GONE);
            buttonTest.setVisibility(View.VISIBLE);
        }
    });

    buttonTest = (Button) convertView.findViewById(R.id.button_test);
    // buttonTest.setVisibility(View.GONE);    <-- remove this line

    buttonTest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // set the state of the first button
            setFirstButtonVisibility(groupPosition, childPosition, true); // true denoting as VISIBLE

            buttonTest.setVisibility(View.GONE);
            buttonOff.setVisibility(View.VISIBLE);
        }
    });

    // depending upon the visibility status of first button, make one of the two visible
    if(buttonOffVisible) {
        buttonOff.setVisibility(View.VISIBLE);
        buttonTest.setVisibility(View.GONE);
    }
    else {
        buttonOff.setVisibility(View.GONE);
        buttonTest.setVisibility(View.VISIBLE);
    }

    TextView listChildText = (TextView) convertView.findViewById(R.id.list_item);

    listChildText.setText(childText);

    return convertView;
}