我有这个 ListView :
<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>
在我的主要 Activity 类(class)中,我放置了以下内容:
contactsView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
arg1.setSelected(true);
selectedContactIndex = (int) arg3;
}
});
contactsView
是 ListView ,我已经确认它被选中但是颜色没有停留。我点击它,它改变然后又变回来。知道为什么吗?
最佳答案
而不是 android:listSelector="@drawable/list_selector"
在 ListView 中使用它作为 row.xml 中的背景
关于Android listview 项目不显示选定的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19193063/