选择新列表项时更改背景颜色 - Android

时间:2015-11-30 22:37:08

标签: java android expandablelistview onclicklistener background-color

更新

我想将所选菜单项的背景颜色更改为蓝绿色。点击后会发生颜色变化,但我仍然无法将上一个菜单项恢复为旧颜色(以显示它已不再被选中)。以下是我所取得的进展,但仍然很短暂。

这是我想要实现的目标,但却无法弄清楚:Change Background Color of Clicked Child in Expandable ListView Android

1。 ChildExpandableListAdapter.java ~stemSelected未被调用。

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。结果         enter image description here

TopicAreaExpandableListAdapter.java 〜更改每个底部子颜色。

@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。结果         enter image description here

0 个答案:

没有答案