【问题标题】:Scrolling a ListView changes everything from White to Black [duplicate]滚动 ListView 将所有内容从白色更改为黑色 [重复]
【发布时间】:2011-03-02 00:22:07
【问题描述】:

我有一个自定义列表视图,我希望列表的背景是白色的,所以我做了这样的事情,效果很好。

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

问题是当你滚动列表时,所有列表项的背景都会变成黑色,这看起来很糟糕。

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

view.setBackgroundColor(Color.WHITE);

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

如何在列表视图中有一个白色背景,在滚动时保持白色,并且正常的列表活动橙色点击背景?

谢谢!

【问题讨论】:

    标签: android


    【解决方案1】:

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

    android:cacheColorHint="#000000"
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 效果很好。 listView = (ListView) this.findViewById(R.id.listview); listView.setBackgroundColor(Color.WHITE); listView.setCacheColorHint(Color.WHITE);
      • listView.setCacheColorHint(Color.TRANSPARENT);或 android:cacheColorHint="#00000000" //重用背景颜色
      • 透明无效,白色有效
      • @max4ever,你能告诉你你的安卓版本吗? 2.3.3。透明效果很好。
      • 有关此问题的更多详细信息,请查看此 android dev 博客文章:android-developers.blogspot.co.nz/2009/01/…
      【解决方案3】:

      如果您的ListView 绘制在更复杂的背景(例如位图或渐变)上,那么您可能需要完全禁用滚动缓存。我最终在我的ListView 上设置了android:scrollingCache="false" 来解决这个问题。

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

      【讨论】:

        【解决方案4】:

        Android 试图通过缓存布局信息来提高 ListView 滚动的性能。如果您有长的滚动数据列表,您还应该在 Activity 的 AXML 定义中的 ListView 声明中设置 android:cacheColorHint 属性(设置为与自定义行布局背景相同的颜色值)。当用户滚动浏览具有自定义行背景颜色的列表时,如果不包含此提示可能会导致“闪烁”。 所以给listitem的背景和android:cacheColorHint赋予相同的颜色。你可以参考下面的代码。

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

        <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"/>
        

        【讨论】:

          【解决方案5】:
          ListView lv = getListView();
          setCacheColorHint(0);
          

          将缓存颜色提示设置为零。

          【讨论】:

            猜你喜欢
            • 2021-01-27
            • 1970-01-01
            • 1970-01-01
            • 2012-07-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-12-18
            • 1970-01-01
            相关资源
            最近更新 更多