【问题标题】:List item long press transition列表项长按过渡
【发布时间】:2013-07-14 05:13:47
【问题描述】:

这是我用于列表项选择器的:

<?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 项目启用了上下文菜单,以便用户可以长按某个项目。我想要的是当用户长按一个项目时,颜色应该从绿色变为红色。我怎样才能做到这一点?

【问题讨论】:

  • 您可以使用Transition(Drawable) 来完成此操作。对于现实生活中的示例,请查看 transition,它是 ActionBarSherlock 中列表选择器的一部分。

标签: android listview android-listview onlongclicklistener long-press


【解决方案1】:

您可以使用 list_selector_background 作为它,建议here

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

并使用过渡进行长按,正如here建议的那样:-

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

也许对你有帮助..

【讨论】:

  • 你弄明白了吗?我有同样的问题,代码看起来不错,但没有发生转换,只有第一种颜色用于长按。
猜你喜欢
  • 2018-03-02
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多