滚动ListView改变从白色到黑色的一切

我有一个自定义列表视图,我想列表的背景是白色的,所以我做这样的工作很好。

listView = (ListView) this.findViewById(R.id.listview); listView.setBackgroundColor(Color.WHITE); 

问题是当你滚动列表时,所有列表项目的背景变成黑色,这看起来很可怕。

我尝试在我的列表视图设置背景颜色为白色。 当我膨胀视图时,我也尝试将背景颜色设置为白色:

 view.setBackgroundColor(Color.WHITE); 

这两个都解决了滚动背景颜色的问题,但是现在该项目看起来并不是可点击的,即使它是。 我的意思是onClick仍然正常工作,但背景不闪烁橙色,让用户知道他点击它。

我怎样才能有一个列表视图中的白色背景,滚动时保持白色,并正常列表橙色点击背景?

谢谢!

解决scheme非常简单,您还需要将caching颜色提示设置为白色: setCacheColorHint(Color.WHITE) 。 您不需要更改列表项目的背景颜色。

 ListView lv = getListView(); setCacheColorHint(0); 

将caching颜色提示设置为零。

解决方法非常简单,您还需要在xml中将caching颜色提示设置为黑色。

 android:cacheColorHint="#000000" 

Android尝试通过caching布局信息来提高ListView滚动的性能。 如果您有长时间滚动的数据列表,则还应该在Activity的AXML定义(与自定义行布局的背景相同的颜色值)上的ListView声明中设置android:cacheColorHint属性。 如果用户在自定义行背景颜色的情况下滚动浏览列表,可能会导致“闪烁”。 所以给listitem的背景和android:cacheColorHint.you可以参考下面的代码。

在我的适配器布局,我给了

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ADAPTER_CONTAINER_PRIMARY" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FAFAEE" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:gravity="center"> ..... 

并在列表视图中

 <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/LIST_GRID_LIST_INSTANCES" android:focusableInTouchMode="true" android:smoothScrollbar="true" android:divider="@drawable/Gray" android:dividerHeight="0.05dp" android:cacheColorHint="#FAFAEE" android:background="@drawable/round"/> 

如果您的ListView绘制在更复杂的背景上 – 比如位图或渐变 – 那么您可能需要完全禁用滚动caching。 我结束了在我的ListView上设置android:scrollingCache="false"来解决这个问题。

http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:scrollingCache