【问题标题】:Android: How to achieve the glow effect when long-pressing a list item?Android:如何实现长按列表项的发光效果?
【发布时间】:2011-09-24 16:37:23
【问题描述】:

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

将选择器替换为下面的选择器会移除效果。根据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>

【问题讨论】:

    标签: android list selector


    【解决方案1】:

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

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

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

    问候, 斯蒂芬

    【讨论】:

    • 对更多任何想法都不起作用,为什么?正如你所说,我应用了转换,你的意思是对于“list_selector_background_transition.xml”文件,我们添加了你在第二个代码块中给出的转换?我确实做到了,但只改变了一种颜色,即只应用了“list_selector_background_pressed”
    • @coderVishal 你使用RecyclerView吗? 此解决方案仅适用于您使用AbsListView 的子类(例如,ListView)。它检查项目背景是否有TransitionDrawable 并启动动画。不幸的是,RecyclerView 并非如此,您必须自己发明一些机制来启动动画。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多