2010-06-10 18 views
51

我有一个自定义列表视图,我希望列表的背景是白色的,所以我做了这样的工作很棒。滚动列表查看会改变从白色到黑色的所有内容

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

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

我试着在我的列表视图中将背景颜色设置为白色。当我吹的观点我也尝试了背景颜色设置为白色:

view.setBackgroundColor(Color.WHITE); 

这两个固定滚动背景颜色的问题,但现在该项目不显示为不可点击,即使它是。我的意思是onClick仍然可以正常工作,但背景不闪烁为橙色,以让用户知道他点击了它。

如何在列表视图中有一个白色背景,滚动时保持白色,并执行普通列表活动橙色单击背景?

谢谢!

回答

118

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

+1

这很好地工作。 listView =(ListView)this.findViewById(R.id.listview); listView.setBackgroundColor(Color.WHITE); listView.setCacheColorHint(Color.WHITE); – pcm2a 2010-06-10 14:04:39

+36

listView.setCacheColorHint(Color。透明);或android:cacheColorHint =“#00000000”//重用背景颜色 – CelinHC 2011-10-20 15:41:51

+0

透明没有工作,白色确实 – max4ever 2012-08-29 13:31:23

6

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

android:cacheColorHint="#000000" 
7
ListView lv = getListView(); 
setCacheColorHint(0); 

设置高速缓存颜色提示为零。

+0

这是一个! – 2013-03-17 21:20:23

3

Android尝试通过缓存布局信息来提高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"/> 
+0

很好的解释 – Sababado 2013-03-18 12:07:51

相关问题