我有一个可绘制的选择器作为每个项目的背景ListView
以突出显示所选行。一切在冰淇淋三明治中都可以正常工作,但在果冻豆中似乎不起作用。找不到任何文档说明哪些更改可能导致它停止工作以及我需要做什么来修复它。
不工作是指当我单击行中的一行时ListView
该项目的背景颜色没有改变@color/blue
颜色,但在 ICS 中是这样。
这是我正在使用的选择器代码(listing_selector.xml):
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@color/blue" />
<item android:state_pressed="true" android:drawable="@color/blue" />
<item android:state_activated="true" android:drawable="@color/blue_selected" />
<item android:state_selected="true" android:drawable="@color/blue_selected" />
<item android:drawable="@android:color/transparent" />
</selector>
这是布局ListView
item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/listing_selector"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
这是蓝色资源:
<resources>
<color name="blue">#ff33b5e5</color>
</resources>
更新1:
尝试将选择器从color
文件夹到drawable
文件夹并将代码更新为:
android:background="@drawable/listing_selector"
更新2:
另外,关于ListView
,尝试添加以下内容:
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:listSelector="@drawable/listing_selector"
/>
更新3:
我认为这可能是我的代码中的某些内容,但我删除了所有代码onListItemClick
of the ListView
并且仍然是selector
不工作。
更新4:
我已经将范围缩小到state_selected
or state_activated
不作为,state_pressed
似乎正在工作
更新5:
我想我错了。我认为选择器根本没有被识别。我对内置的感到困惑ListView
突出显示为我的选择器。我现在想知道这是否与我的项目设置方式有关。我在库类中有选择器。也许从 ICS 到 JB 发生了一些变化,但是将选择器移动到我的应用程序的项目似乎并没有解决它。
更新6:
好吧,经过更多的拉扯之后,我再次将范围缩小到state_selected
or state_activated
不被识别,因为改变了颜色state_pressed
确实有效,这意味着我的选择器正在被识别。从评论来看,这似乎与我的应用程序有关,因为其他人已经能够让选择器与 Jelly Bean 一起使用。
尽管其他有趣的事情是改变drawable
无法识别默认状态的值。我在哪里有color/transparent
,我认为将其更改为某种颜色会导致列表更改为该颜色,但事实并非如此。
此外,这在 ICS 中也不起作用。
更新7:
经过更多的努力后,我发现长按菜单项会导致该项目的颜色发生变化。仅单击某个项目仍然不起作用。甚至不知道这意味着什么。
** 最终更新:**
我放弃了,我删除了选择器,只是刷新了ListView
单击并记住单击的位置并从代码中突出显示它。不理想,但不值得花精力尝试修复。