如何更改ExpandableListView项的背景

时间:2014-05-20 00:44:55

标签: android expandablelistview android-selector

我有一个ExpandableListView。 当孩子被扩大时,应该考虑"选择"。选择孩子时,孩子和孩子的孩子都应该显示不同的背景(向用户显示当前所选项目)。

现在这就是我所拥有的:

XML

    <ExpandableListView
        android:id="@+id/listViewAccepted"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:listSelector="@drawable/custom_selector" >
    </ExpandableListView>

ListView

        setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View view,int groupPosition, long id) {
                view.setSelected(true);
                parent.setItemChecked(groupPosition, true);
                return false;
            }
        });

选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="@color/holo_blue_light" />
        </shape>
    </item>
    <item android:state_selected="true">
        <shape>
            <solid android:color="@color/green" />
        </shape>
    </item>
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/green" />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape>
            <solid android:color="@color/green" />
        </shape>
    </item>
    <item android:state_activated="true">
        <shape>
            <solid android:color="@color/green" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

1 个答案:

答案 0 :(得分:5)

以下是我如何做到的,以下更改了组视图。重写的方法包含在我的自定义ExpandableListAdapter中。

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View view, ViewGroup parent)
{
    LinearLayout bevGroup = (LinearLayout) view.findViewById(R.id.myid);

    if(isExpanded)
    {
        bevGroup.setBackgroundColor(context.getResources().getColor(R.color.bgGroupBlack));
    }
    else
    {
        bevGroup.setBackgroundColor(context.getResources().getColor(R.color.bgTransparent));
    }
}

孩子,只有在展开时才能看到,总是它应该是的颜色。