突出显示ListView中的神秘行为

Max*_*me 5 android android-listview

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

这是我的ListView:

<ListView android:id="@+id/TopListView" android:layout_width="fill_parent"
    android:listSelector="@drawable/regular_selector"
    android:layout_height="wrap_content">
</ListView>
Run Code Online (Sandbox Code Playgroud)

它是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>
Run Code Online (Sandbox Code Playgroud)

sco*_*yab 8

当使用颜色作为背景时,我在整个列表视图中突出显示相同的问题.奇怪的是,这只发生在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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


gwv*_*eri 0

我想这是常见的问题。您必须在列表视图中进行设置:

android:cacheColorHint="#00000000"
Run Code Online (Sandbox Code Playgroud)

目标是禁用框架为提高滚动操作期间的绘图性能而进行的优化。