ListView中的StateListDrawable在某些条件下奇怪地变色

时间:2010-10-29 19:19:15

标签: android listview colors

我创建了一个带有TextViews的ListView作为项目 - 其中大部分都有一定颜色作为背景,但有些具有自定义@ drawable / shape作为背景(即顶部和底部项目,它们具有圆角)。 这些背景设置为状态列表的一部分,以便在点击项目时提供另一种背景颜色。 ListView的listSelector设置为透明,而项目中包含的TextViews设置为可点击。

的ListView:

<ListView android:id="@+id/allreminders_list" android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:dividerHeight="3px" android:cacheColorHint="#00000000"
android:divider="@color/trans" android:background="@drawable/reminderlist_background"
android:listSelector="@android:color/transparent"/>

ListView条目:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/entry_container">
<TextView android:id="@+id/remindername" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="dummy"
android:padding="15dp" android:layout_marginLeft="15px"
android:layout_marginRight="15px" style="@style/ReminderEntry"
android:clickable="true"/>
</RelativeLayout>

ListView条目背景的编程设置:

if (position == 1)
textViewReminderName
  .setBackgroundResource(R.drawable.reminder_entry_state_top);
else if (position == remindersInSection)
textViewReminderName
  .setBackgroundResource(R.drawable.reminder_entry_state_bottom);
else
textViewReminderName
  .setBackgroundResource(R.drawable.reminder_entry_state);

顶级项目的状态列表(R.drawable.reminder_entry_state_top):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/rounded_top_corners_selected" />
<item android:state_selected="true" android:drawable="@drawable/rounded_top_corners_selected" />
<item android:drawable="@drawable/rounded_top_corners" />

未选顶项的形状(@ drawable / rounded_top_corners):

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="@color/entry_background"/>    
<corners android:bottomRightRadius="0dp" android:bottomLeftRadius="0dp" 
 android:topLeftRadius="5dp" android:topRightRadius="5dp"/> 
</shape>

这个东西在功能上有效。但是,在某些条件下,顶部和底部项目(具有可绘制形状的背景,如上所示)会略微改变颜色:

  • 向下滚动列表而不停止滚动
  • 点击列表中的任何项目后

这些项目的正确颜色仅在滚动列表时滚动列表而不移动手指时显示。

这可能是什么? StateListDrawables呈现可绘制形状的方式中的错误?

1 个答案:

答案 0 :(得分:0)

经过无数的研究和猜测,我自己找到了原因:抖动! 通过在状态列表选择器中禁用抖动,问题神奇地消失了:

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:dither="false">

似乎抖动仅在某些条件下应用(如上所述),这是恕我直言,而不是预期的行为,但可通过完全禁用它来治疗。