找到了答案。我的问题不是选择器的状态,我希望所有项目的选择器处于相同状态!问题是,如果适配器中的某些布尔值为假,我希望该项目是常规的,如果布尔值为真,则颜色为 X,但即使颜色为 X,我仍然希望它在被选中或按下后是透明的。
所以解决方案是以下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="@android:color/transparent" /> <!-- pressed -->
<item android:state_selected="true"
android:drawable="@android:color/transparent" /> <!-- focused -->
<item android:drawable="@color/viewed_hotels_color" /> <!-- default -->
</selector>
我发现即使列表本身是获取状态的列表,它确实将状态传递给它的子项(即列表项),我认为它没有这样做。只有 android:focused 在这里无效,android 列表项没有被聚焦而是被选中......所以你可以看到在任何没有被按下或选择的状态下我给我的默认颜色,否则它是透明的并且选择器对所有人都是可见的.
在我的适配器中,我需要以下代码(在 getView() 方法中):
View itemView = convertedView; <- i'll spare you the layout details...
Item item = getItem(position);
if( item.isViewed() ){
itemView.setBackgroundResource(R.drawable.viewed_item_background);
}else{
itemView.setBackgroundDrawable(null);
}
这可以确保每个项目都获得正确的背景。当然,如果项目状态更改为已查看并且我希望它显示我需要在适配器上调用 notifyDatasetCahnged()。