简体   繁体   中英

How to change background of ExpandableListView's items

I have an ExpandableListView. When a child is expanded it should be considered "selected". When a child is selected both the child and the child's children should display a different background (to show the user which is the current selected item).

Right now this is what I have:

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>

The 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;
            }
        });

The Selector

<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>

Here's how i did it, the following changes the group view. The overridden method is contained in my custom 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));
    }
}

And the child, only ever being visible when expanded, is always the color it's supposed to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM