你好我有一个奇怪的问题我想在列表视图中突出显示选定的项目
这是我的 xml 文件
选定的.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/header" />
<item android:state_focused="true" android:drawable="@color/selected" />
</selector>
listitem_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/android:transparent" />
<item android:drawable="@drawable/selector" />
</selector>
托管 ListView 的活动
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:background="@drawable/listitem_background" >
...
<ListView
android:id="@+id/carsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/carConfirmBtn"
android:layout_below="@id/carSelect"
android:paddingLeft="@dimen/small"
android:paddingRight="@dimen/small"
android:listSelector="@drawable/selector" />
</RelativeLayout>
最后是定义它的代码片段
carsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
carsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
carsList.setSelection(pos);
}
});
请帮忙