- 要突出显示选定的项目,您应该为 listView 请求焦点并在 UI 线程中运行 setSelection()。这对我有用:
runOnUiThread(new Runnable() {
public void run() {
myList.requestFocus();
myList.setSelection(position);
}
});
2.将特定颜色应用于项目。您可以使用自定义的 listItem。
一个。为您的 listView 设置自定义的 lisItem:
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.customizedlistitem,arrListView);
myList.setAdapter(listAdapter);
b.在你的布局文件夹中,创建customizedlistitem.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customizedlistitem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/customizedbackground"/>
c。在你的drawable文件夹中,像这样创建customizedbackground.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_pressed="false" android:drawable="@color/RED"></item>
<item android:state_pressed="true" android:drawable="@color/GREEN"></item>
<item android:drawable="@color/BLACK"></item> <!-- for other state -->
</selector>
d。确保在您的项目中定义了颜色 RED、GREEN 和 BLACK(您可以在 values 文件夹下的 color.xml 中定义它):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="RED">#FF0000</color>
<color name="GREEN">#008000</color>
<color name="BLACK">#000000</color>
</resources>