2011-12-30 208 views
1

我有我的应用程序一个奇怪的行为...一个有一个ListView,其中奇数项具有#ffffff背景,甚至项目具有#f2f7f7,看起来像这样:ListView的项目更改背景颜色

Activity start OR touching list (without release)

然而,当我开始滚动列表,偶数项略有变色,成为#f7f7f7,像这样:

When scrolling and after scroll release

有任何想法吗?如果您需要更多信息,请询问。我对这种行为感到困惑。

附加信息:

我设置每个项目的背景色getView()方法,像这样:

protected final int[] colors = new int[] { R.drawable.states_list_odd, R.drawable.states_list_even }; 
... 
// Alternates list items' background 
int colorPos = position % colors.length; 
convertView.setBackgroundResource(colors[colorPos]); 

回答

4

简单的东西,实际上。 android:scrollingCache="false"解决了这个问题。

2

用于当您滚动列表中被重复使用项目的意见(没有必要创建新的)。因此,当您向下滚动列表时,位置0处的项目可能会呈现在位置10.它将是相同的View对象,并且在显示getView之前将使用设置为此特定视图的convertView参数进行调用 - 因此您可以填充它与数据(并指定您的样式)。这只是一个优化。

因此,每次在您的列表适配器中调用getView时,您都需要指定该项目的背景颜色。

+0

我更新了我的问题,以显示如何在'getView()'中设置每个项目的背景颜色。 – gnclmorais 2011-12-30 17:21:03

1

以编程方式尝试此操作。 yourListView.setScrollingCacheEnabled(false);