我有一个ExpandableListView定义为:
<ExpandableListView
android:id="@+id/MyListView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:listSelector="@drawable/mylistview_selectable"
/>
drawable / mylistview_selectable.xml是:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="#FF4" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#4FF" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#D0F" />
</shape>
</item>
</selector>
并且ExpandableListView的OnChildClickListener具有:
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
MyListView.setSelectedChild(groupPosition, childPosition, true);
return true;
}
GameList为:
ExpandableListView MyListView = (ExpandableListView)findViewById(R.id.MyListView);
但是,当我按下子项时,背景会短暂变为state_pressed
颜色,然后再返回默认背景。 state_focused
和state_selected
颜色都没有出现。
我尝试使用setItemChecked
和state_checked
,但这也不起作用。
如何设置“已选择”项目的背景(以便在不再选择时自动取消设置)?