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

用下面的选择器代替选择器可以消除效果。根据this question,我需要一个动画来复制它。我将如何在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>

最佳答案

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

找到on the web

它使用这种转换来长按一下鼠标:
<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>

找到on the web too

没有动画。记住要使您的状态保持一致,或者如果交换它们,至少要考虑一下,顺序很重要。

就个人而言,我喜欢事物以标准方式运行,因此我只允许标准列表选择器使用。

问候,
斯特凡

关于Android:长按列表项时如何实现发光效果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6513301/

10-13 03:03