开发者

Android: How to achieve the glow effect when long-pressing a list item?

开发者 https://www.devze.com 2023-03-15 21:04 出处:网络
With the default selector, long-pressing a list item causes its background to transition between two colors.

With the default selector, long-pressing a list item causes its background to transition between two colors.

Replacing the selector with the one below removes the effect. According to this question, I need an animation to reproduce it. How would I go about doing that in 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>


Here is the code from 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>

Found on the web.

And it uses this transition for long press clicks :

<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>

Found on the web too .

There is no animation for that. And remember to keep you states in the same order, or at least think about it if you swap them, order is important.

Personnally, I like when things behave in a standard way, so I would just let the standard list selector.

Regards, Stéphane

0

精彩评论

暂无评论...
验证码 换一张
取 消