列表项长按过渡效果

3

这是我用来选择列表项的代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item android:drawable="@color/red" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@color/green" android:state_pressed="false"/>
<!-- normal -->
</selector>

我已经为ListView项目启用了上下文菜单,以便用户可以长按项目。我想要的是,当用户长按一个项目时,颜色应该从绿色变为红色。我该怎么做呢?


1
你可以使用Transition(Drawable)来实现这个功能。一个真实的例子,可以看一下ActionBarSherlock中列表选择器的transition - MH.
1个回答

2
你可以使用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>

按照建议,在长按时使用过渡效果,如此处所示:

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

也许这会对您有所帮助...

你解决了吗?我也遇到了同样的问题,代码看起来没问题,但是转换没有发生,只有第一个颜色在长按时被使用。 - Björn Kechel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接