Android:长按列表项时如何实现发光效果?

10 android list selector

使用默认选择器,长按列表项会导致其背景在两种颜色之间转换.

用下面的选择器替换选择器会消除效果.根据这个问题,我需要一个动画来重现它.我将如何在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>
Run Code Online (Sandbox Code Playgroud)

Sni*_*las 9

以下是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>
Run Code Online (Sandbox Code Playgroud)

在网上找到.

它使用此转换进行长按点击:

<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>
Run Code Online (Sandbox Code Playgroud)

也可以在网上找到.

没有动画.并且记得以相同的顺序保持状态,或者如果你交换它们至少要考虑它,顺序很重要.

个人而言,我喜欢当事物以标准方式运行时,所以我只想让标准列表选择器.

此致,Stéphane

  • 为什么更多的想法不起作用?我按照你的说法应用了转换,你的确意味着对于"list_selector_background_transition.xml"文件,我们添加了你在第二个代码块中给出的转换?我完全这样做,但只有一种颜色被修改,只应用了"list_selector_background_pressed" (2认同)