android - 列表项长按转场

标签 android listview android-listview onlongclicklistener long-press

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

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

最佳答案

您可以按原样使用 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>

也许它会对你有所帮助..

关于android - 列表项长按转场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669494/

相关文章:

android - 调用 stopService 后服务正在运行

android - 带有自定义项的底部导航 View (actionLayout)

android - 如何判断ListView的滚动方向

android - 在 Android 应用程序中实现应用程序内购买是否需要启用实时开发者通知?

android - ListView 多选

android - ListView 中最后一项的 ImageViews 的奇怪行为

android - 带有 CursorAdapter : wrong position when scroll 的聊天气泡 ListView

android - 如何将 ListView 行中的 TextView 中的值放入 fragment

android - 如何处理 ListView 中的多个倒数计时器?

android - 如何使用选择器设置初始 ListView 项目背景颜色