Android 滚动 ListView 会将所有内容从白色更改为黑色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3011945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:17:07  来源:igfitidea点击:

Scrolling a ListView changes everything from White to Black

android

提问by Cameron McBride

I have a custom list view and I want the background of the list to be white, so I do something like this which works great.

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

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

The problem is when you scroll the list the background of all the list items changes to black, which looks horrible.

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

I tried in my list view setting the background color to white. When I inflate the view I also tried setting the background color to white:

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

view.setBackgroundColor(Color.WHITE);

Both of these fix the problem of the scrolling background color, but now the item doesn't appear to be clickable even though it is. What I mean by that is the onClick still works fine, but the background doesn't flash to orange to let the user know he clicked it.

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

How can I have a white background in a list view, that stays white while scrolling, and does the normal list acitvity orange click background?

如何在列表视图中使用白色背景,在滚动时保持白色,并且正常的列表活动是否为橙色单击背景?

Thanks!

谢谢!

回答by Romain Guy

The solution is very simple, you also need to set the cache color hint to white: setCacheColorHint(Color.WHITE). You don't need to change the list items' background color.

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

回答by Sababado

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

Set the cache color hint to zero.

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

回答by Google

Solution is very simple, you also need to set the cache color hint to black in xml.

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

android:cacheColorHint="#000000"

回答by Tripaty Sahu

Android attempts to improve the performance of ListView scrolling by caching layout information. If you have long scrolling lists of data you should also set the the android:cacheColorHint property on the ListView declaration in the Activity's AXML definition (to the same color value as your custom row layout's background). Failure to include this hint could result in a ‘flicker' as the user scrolls through a list with custom row background colors. So give the same color to the listitem's background and android:cacheColorHint.you can refer below code.

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

In my Adapter layout I gave as

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

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

And in the List view

并在列表视图中

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

回答by Leo Accend

If your ListViewis drawn atop a more complex background -- say, a bitmap or gradient -- then you may need to disable the scroll cache entirely. I ended up setting android:scrollingCache="false"on my ListViewto resolve this issue.

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

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

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