我有这个列表视图:
<ListView
android:id="@+id/contactsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/searchButton"
android:divider="@color/DarkGoldenrod"
android:dividerHeight="0.1dp"
android:listSelector="@drawable/list_selector"
android:choiceMode="singleChoice" />
这是代码list_selector
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:drawable/myRedColor" android:state_pressed="true"/>
<item android:drawable="@android:drawable/myBlueColor" android:state_selected="true"/>
<item android:drawable="@android:drawable/myGreenColor"/>
</selector>
在我的主要活动课程中,我提出了以下内容:
contactsView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
arg1.setSelected(true);
selectedContactIndex = (int) arg3;
}
});
contactsView
是列表视图,我已经确认它被选中但颜色没有保留。我点击它,它会改变然后又变回来。知道为什么吗?