2011-06-28 12 views
10

Przy domyślnym selektorze długie naciśnięcie elementu listy powoduje przejście tła między dwoma kolorami.Android: jak uzyskać efekt blasku podczas długiego naciśnięcia elementu listy?

Wymiana selektora za pomocą poniższego usuwa efekt. Według this question potrzebuję animacji, aby ją odtworzyć. Jak mam to zrobić w xml?

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_pressed="true"> 
     <shape> 
      <solid 
       android:color="@color/state_pressed" /> 
     </shape> 
    </item> 
    <item 
     android:state_focused="true"> 
     <shape> 
      <solid 
       android:color="@color/state_focused" /> 
     </shape> 
    </item> 
    <item> 
     <shape> 
      <solid 
       android:color="@color/state_idle_grey" /> 
     </shape> 
    </item> 
</selector> 

Odpowiedz

9

Oto kod z list_selector_background:

<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" /> 
     <item android:state_focused="true" android:state_pressed="true" 
       android:drawable="@drawable/list_selector_background_transition" /> 
     <item android:state_focused="false" android:state_pressed="true" 
       android:drawable="@drawable/list_selector_background_transition" /> 
     <item android:state_focused="true" 
       android:drawable="@+drawable/list_selector_background_focus" /> 
</selector> 

Znaleziony on the web.

i używa tego przejścia do długich kliknięć Press:

<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/list_selector_background_pressed" /> 
    <item android:drawable="@drawable/list_selector_background_longpress" /> 
</transition> 

Znaleziono on the web too.

Nie ma animacji. I pamiętaj, aby utrzymywać stany w tej samej kolejności lub przynajmniej myśleć o tym, jeśli je zamienisz, kolejność jest ważna.

Osobiście lubię, gdy rzeczy zachowują się w standardowy sposób, więc po prostu pozwolę selektorowi listy standardowej.

Pozdrawiam, Stéphane

+0

Dziękuję, że jest doskonała. –

+2

nie działa więcej pomysłów, dlaczego? Zastosowałem przejście, jak powiedziałeś, co oznaczało, że dla pliku "list_selector_background_transition.xml" dodajemy przejście, które dałeś w drugim bloku kodu? Zrobiłem to dokładnie, ale tylko jeden kolor jest śledzony, a tylko "list_selector_background_pressed" jest stosowany – coderVishal

+0

@coderVishal Czy używasz 'RecyclerView'? ** To rozwiązanie działa tylko wtedy, gdy używasz podklasy 'AbsListView' ** (na przykład ListView). Sprawdza, czy tło elementu ma 'TransitionDrawable' i uruchamia animację. Niestety nie jest tak w przypadku 'RecyclerView', musisz wymyślić jakiś mechanizm, aby samemu rozpocząć animację. – Malcolm

Powiązane problemy