3

Updated

I want to change the background color of the selected menu item to teal. The color change happens when it's clicked, but I'm still having trouble returning the previous menu item to the old color (to show it's not selected anymore). Below is the progress I've made but still coming up short.

This is what I'm trying to achieve but just can't figure it out: Change Background Color of Clicked Child in Expandable ListView Android

1. ChildExpandableListAdapter.java ~ itemSelected not getting called.

public class ChildExpandableListAdapter extends BaseExpandableListAdapter {
    private Integer selectedPosition;

int itemSelected = -1;

public void setSelected(int selected) {
    itemSelected = selected;
}

public void setSelectedPosition (int position) {
    this.selectedPosition = position;
}

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

    if (childPosition == itemSelected) 
        currentView.setBackgroundColor(activity.getResources().getColor(R.color.hpoe_teal));
    else 
        currentView.setBackgroundColor(activity.getResources().getColor(R.color.hpoe_light_light_blue));

}

1. Result enter image description here

TopicAreaExpandableListAdapter.java ~ Changes each bottom Child color.

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

    if (isExpanded) {
        int color = currentView.getResources().getColor(R.color.hpoe_teal);
        currentView.setBackgroundColor(color);
        currentView.invalidate();
    }
    else {
        int color = currentView.getResources().getColor(R.color.hpoe_light_light_blue);
        currentView.setBackgroundColor(color);
    }
}

2. Result enter image description here

Community
  • 1
  • 1

0 Answers0