【问题标题】:Android - Disable ListView Selection Highlight but Keep OnClick enabled [duplicate]Android - 禁用 ListView 选择突出显示但保持启用 OnClick [重复]
【发布时间】:2013-06-06 16:01:33
【问题描述】:

我想禁用当用户从代码中选择一行 (listSelector) 时出现的突出显示。我不想禁用 onClick 和 enabled 设置(我还是想听点击,只是想去掉高亮)。

【问题讨论】:

  • 您使用的是哪个适配器?
  • 一个简单的 ArrayAdapter。重要吗?
  • “我想禁用用户选择一行时出现的突出显示”——为什么?那些使用箭头键、方向键等来导航您的应用程序的用户呢?请允许所有用户使用您的应用,而不仅仅是那些使用触摸屏的用户。
  • stackoverflow.com/a/4075045/1005846。希望它会有所帮助
  • @CommonsWare 好吧,我不想支持硬键盘,我不会:)

标签: java android listview


【解决方案1】:

我是这样做的:

通过添加 ListView 的两个属性。

android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent"

您的 ListView 应如下所示:

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:cacheColorHint="@android:color/transparent"
    android:listSelector="@android:color/transparent">

</ListView>

完成

【讨论】:

    【解决方案2】:

    只需创建一个具有透明颜色的可绘制对象,如下所示:

    <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_disabled_holo_light" />
    <item android:state_focused="true"  android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_light" />
    <item android:state_focused="true"  android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="true"  android:drawable="@drawable/list_focused_holo" />
    
    </selector>
    

    然后通过代码或XML设置:

    listView.setSelector(R.drawable.my_transparent_selector);
    

    这个方法的 javadoc 说:

    设置一个用于高亮当前选中项的 Drawable。

    XML 属性为:

    android:listSelector

    你可以玩所有的状态,记住你也有焦点状态。

    【讨论】:

    • 来自代码,Nicolas,而不是 XML。我已经可以使用 listSelector 属性从 XML 处理它了。
    • 我不知道你为什么需要这样,所以保留两个文件并玩它们。
    • 不,编辑后,它工作正常。我正在使用某种复选框而不是行突出显示。你的回答就像一个魅力。谢谢。
    • 太棒了!很高兴能帮到你!
    【解决方案3】:

    试试listview.setSelector(new ColorDrawable(Color.TRANSPARENT));

    【讨论】:

    • 这对我来说是最简单的,感谢他采用程序化方法。
    【解决方案4】:

    在您的 ListView XML 中指定 android:listSelector="@android:color/transparent"

    【讨论】:

    • 非常简单,而且成功了!
    【解决方案5】:

    高亮效果是listSelector上的一种样式。您可以覆盖 listSelector 样式。

    这是一个带有列表视图的示例:Android: disabling highlight on listView click

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 2011-03-10
      • 2014-11-06
      • 2012-02-19
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      相关资源
      最近更新 更多