Drawable selector not working in Jelly Bean

久未见 提交于 2019-12-06 18:26:12

问题


I have a drawable selector as a background for each item in a ListView to highlight the selected row. Eveything works fine in Ice Cream Sandwich, but doesn't seem to work in Jelly Bean. Can't find any documentation saying what changes could have caused it to stop working and what I need to do to fix it.

By not working, I mean when I click on a row in the ListView the item's background color isn't turning the @color/blue color, but it does in ICS.

This is the selector code I'm using (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>

This is the layout of the 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>

This the blue color resource:

<resources>
    <color name="blue">#ff33b5e5</color>       
</resources>

UPDATE 1:

Tried moving the selector from the color folder to the drawable folder and updating the code to this:

android:background="@drawable/listing_selector"

UPDATE 2:

Also, on the ListView, tried adding this:

    <ListView android:id="@android:id/list"
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent" 
        android:listSelector="@drawable/listing_selector"   
    />

UPDATE 3:

I thought it might be something in my code, but I removed all the code from onListItemClick of the ListView and still the selector isn't working.

UPDATE 4:

I've narrowed it down to state_selected or state_activated not working as, state_pressed seems to be working

UPDATE 5:

I think I was mistaken. I don't think the selector is being recognized, at all. I was confusing the built-in ListView highlighting as my selector. I'm now wondering if it has something to do with the way my project is setup. I have the selector in a Library Class. Maybe something changed with that from ICS to JB, however moving the selector to my app's project didn't seem to fix it.

UPDATE 6:

Ok, after some more hair pulling, I've narrowed it down, again, to either state_selected or state_activated not being recognized, as changing the color for state_pressed does work, which means my selector is being recognized. From the comments in seems to be something with my app specifically as others have been able to get selectors working with Jelly Bean.

Though something else that is interesting is that changing the drawable value for the default state is not recognized. Where I have color/transparent, I would think changing that to a color would cause the listing to change to that color, but it doesn't.

Also, this isn't working in ICS either.

UPDATE 7:

After even more hair pulling, I've discovered that long-pressing on a menu item results in that item's color being changed. Just clicking on an item still does not work. Not even sure what the means.

** Final Update:**

I give up, I removed the selector and am just refreshing the ListView on click and remembering the position clicked and highlighting it from code. Not ideal, but not worth the effort to try to fix.


回答1:


Okay, i think its an issue with your selector. Try removing the state_focused and the state_activated. You could try this for your selector:

<!-- Disabled State -->
<item
    android:state_enabled = "false"
    android:state_focused = "true"
    android:state_pressed = "true"
    android:drawable="@android:color/transparent">
</item>
<item
    android:state_enabled = "false"
    android:state_focused = "true"
    android:drawable="@android:color/transparent">
</item>

<!-- Pressed State -->
<item
    android:state_pressed = "true"
    android:state_focused = "true">
    <shape>
        <solid android:color="@color/blue"/>
    </shape>
</item>
<item
    android:state_pressed = "true"
    android:state_focused = "false">
    <shape>
        <solid android:color="@color/blue"/>
    </shape>
</item>

<!-- Normal State -->
<item
    android:drawable="@android:color/transparent">
</item>

I found that I needed to use a shape object too instead of android:drawable, because on pre-ICS phones, the whole list will be highlighted that color instead of the pressed list item.

You can add in your state_selected code too, but I'm not sure how it will be used. Check out the default selector code for jelly bean for the states they use: list_selector_background.xml.




回答2:


listing_selector.xml must be in res/drawable folder and set the android:background attribute of your RelativeLayout like this:

<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="@drawable/listing_selector"    
>



回答3:


I cant say the reason why its working in one version and not working in another, but I got a alternative solution.

Define your color as a drawable in resources

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="blue">#00F</drawable>
    ...
</resources>

Use this color drawable in your selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/blue" />
    ...
    ...
</selector>

Try this and let me know.




回答4:


I think it works fine !!

your selector xml file code:-

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

        <item android:drawable="@color/gray" android:state_focused="true"/>
        <item android:drawable="@color/gray" android:state_pressed="true"/>
        <item android:drawable="@color/orange" android:state_activated="true"/>
        <item android:drawable="@color/orange" android:state_selected="true"/>
        <item android:drawable="@color/orange"/>

    </selector>

now create a folder values-v16 under res folder and put this colors file in it.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="gray">#4f4f4f</color>
    <color name="orange">#e26c0e</color>

</resources>

and then the listview :

<ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:listSelector="@drawable/listing_selector" >
</ListView>

Hope this works ! Tested on version 4.1




回答5:


From the API Docs:

Note: Remember that Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).

Thus, try to reorder the states of your selectors according to the suggested order. In your case that would be this:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_pressed="true" android:drawable="@color/blue" />
   <item android:state_focused="true" android:drawable="@color/blue" />
   <item android:state_selected="true" android:drawable="@color/blue_selected" />
   <item android:state_activated="true" android:drawable="@color/blue_selected" />  
   <item android:drawable="@android:color/transparent" />
</selector>


来源:https://stackoverflow.com/questions/11661190/drawable-selector-not-working-in-jelly-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!