2011-12-13 15 views
5

Ustawiłem niestandardowy selektor listy dla Prostego ListView, ale kiedy wybiorę element, cały ListView zmienia kolor na niebieski. Nie rozumiem, gdzie jest problem.Wyróżnij tajemnicze zachowanie w ListView

To mój ListView:

<ListView android:id="@+id/TopListView" android:layout_width="fill_parent" 
    android:listSelector="@drawable/regular_selector" 
    android:layout_height="wrap_content"> 
</ListView> 

i jest to regular_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" android:state_focused="true" 
     android:drawable="@color/transparent" /> 
    <item android:state_pressed="true" 
     android:drawable="@color/blue" /> 
    <item android:state_focused="true" 
     android:drawable="@color/blue" /> 
</selector> 
+0

więc dostałem soln? jeśli nie, możesz powiedzieć, jaki jest twój oczekiwany wynik? – havexz

Odpowiedz

0

Myślę, że to jest zwykle problemem. Trzeba ustawić to w ListView:

android:cacheColorHint="#00000000" 

Celem jest, aby wyłączyć optymalizację że ramy robi dla poprawy wydajności rysowania podczas operacji przewijania.

+0

To ten sam wynik ... – Maxime

0

Po ustawieniu pliku regular_selector.xml jako tła elementu listview działa!

8

Podczas korzystania z koloru jako tła miałem ten sam problem z podświetlaniem całego widoku listy. Co ciekawe ten tylko się poniżej api 11.

rozwiązaniem było używać zwartego kształtu rozciągliwej zawinąć kolor:

list_selector_shaped_background_press.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="@color/list_selector_pressed"/> 
</shape> 

List_selector_background.xml

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

     <item android:state_window_focused="false" 
     android:drawable="@android:color/transparent" /> 

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_disabled" /> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:drawable="@drawable/list_selector_background_disabled" /> 

    <!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects --> 
    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_shaped_background_pressed" /> 

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

    <item android:state_focused="true" 
     android:drawable="@drawable/list_selector_background_focus" /> 

</selector> 
+0

Wielkie dzięki ... Szukałem rozwiązania od kilku tygodni. To naprawdę pomogło! :) – Scrat

Powiązane problemy