Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Android: Entire ListView changes color on focus, not just ListView child item

I am trying to apply a selector to a ListView to make it easy for those without touch screens to navigate my app. The thing is, by applying the selector to the ListView, it only seems to apply the background colors to the entire list, not the items inside of it.

Any ideas? Here's some code:

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

   <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="There are no Clients yet."
        />

listselector.xml in drawable folder:

    <?xml version="1.0" encoding="utf-8"?>
<selector
    android:id="@+id/myselector"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item
        android:state_focused="false"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@color/darkblue" />
    <item
        android:state_focused="false"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@color/green" />

    <!-- Focused states -->
    <item
        android:state_focused="true"
        android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@color/green" />
    <item
        android:state_focused="true"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@color/green" />

    <!-- Pressed -->
    <item
        android:state_pressed="true"
        android:drawable="@color/green" />
</selector>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason you're seeing the focused colour being applied to the whole list is because you have directly referenced a colour value for the focused and pressed states. There is a known bug in Android v2.3 and below where the colour drawable doesn't honour its bounds in these circumstances.

To fix this issue you can create a shape drawable using the desired colours and reference that instead.

e.g define the shape in 'drawables/list_selector_focused.xml' as:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@color/green" />

</shape>

And then reference that in the selector:

<item android:state_focused="true"
      android:state_selected="true"
      android:state_pressed="false"
      android:drawable="@drawable/list_selector_focused" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...