android - 突出显示 ListView 中的神秘行为

标签 android android-listview

我已经为简单 ListView 设置了一个自定义列表选择器,但是当我选择一个项目时,整个 ListView 变成蓝色。我不明白问题出在哪里。

这是我的 ListView :

<ListView android:id="@+id/TopListView" android:layout_width="fill_parent"
    android:listSelector="@drawable/regular_selector"
    android:layout_height="wrap_content">
</ListView>

这是 regular_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@color/transparent" />
  <item android:state_pressed="true"
        android:drawable="@color/blue" />
  <item android:state_focused="true"
        android:drawable="@color/blue" />
</selector>

最佳答案

当使用颜色作为背景时,我遇到了整个 ListView 突出显示的相同问题。奇怪的是,这只发生在 api 11 以下。

解决方案是使用可绘制的实体形状来包裹颜色:

list_selector_shaped_background_press.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@color/list_selector_pressed"/>
</shape>

List_selector_background.xml

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

    <!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects -->
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_shaped_background_pressed" />

        <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/list_selector_shaped_background_pressed" />

    <item android:state_focused="true"
        android:drawable="@drawable/list_selector_background_focus" />

</selector>

关于android - 突出显示 ListView 中的神秘行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8493525/

相关文章:

android - 无法取消订阅firebase云消息传递android

android - 如何在我的 Android 应用程序上实现推荐模块?

android - Intent.getType() 在 onNewIntent(Intent intent ) 方法中返回 null

带有自定义适配器的 Android ListView 多个数组

android - 有谁知道如何在 Android listView 中突出显示所选项目?

android - onContextItemSelected 为 fragment 调用了两次

android - 打开输出流编码器时出错 #0 :0 for Creating Video from image, Gif,music

java - 使用android的JobScheduler按一定顺序完成任务

android - ListView : How to display item selector when rows have clickable widgets?

java - 添加附加项目后 ListView 不刷新