突出显示ListView中的神秘行为

时间:2011-12-13 17:31:32

标签: 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>

3 个答案:

答案 0 :(得分: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>

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>

答案 1 :(得分:0)

我想这是通常的问题。你必须在listview中设置它:

android:cacheColorHint="#00000000"

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

答案 2 :(得分:0)

当我将regular_selector.xml设置为listview项目后台时,它可以工作!