当滚动时,背景ListView变成黑色

431

我创建了一个特定的列表,其中包含以下元素,以创建一个可滚动的列表,每一行左侧都包含一张图片,右侧则是一些文本:

首先创建一个“根”布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="#C8C8C8"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:divider="#C8C8C8"
        android:background="#C8C8C8"/>
</LinearLayout>

然后在ListView中,我放置了以下的“row”项:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/bg_row"
>
    <ImageView
        android:layout_width="wrap_content"
        android:paddingLeft="10px"
        android:paddingRight="15px"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_image"
    />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5px"
        android:paddingBottom="5px"
        android:textSize="16sp"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:maxHeight="50px"/>
</LinearLayout>
只要屏幕静态显示(即没有滚动),它就会正确显示,但是当我开始浏览列表时,行项的背景(在代码中可以显示为“图标”)将被正确显示,但是“根”布局的背景将变成完全黑色...当滚动停止时,背景大多数时候会恢复其颜色...作为测试,我还在该根元素中添加了一个带有相同背景的 TextView,在列表滚动时,这个 TextView 将保持其颜色...有什么想法,以及如何解决这个问题吗?
12个回答

771
ListView 标签上添加属性。
android:cacheColorHint="#00000000" // setting transparent color

要了解更多详情,请查看此博客


23
这段代码意思是:设置ListView的缓存颜色为透明。在布局文件中无法设置颜色,但是使用这行代码可以实现。我将这行代码放在列表活动的'onCreate'方法中使用。希望这些信息也能对您有所帮助。 - Dijo David
70
如果您不喜欢使用0,可以将“android:cacheColorHint =“#0000””用作替代选项。请注意,此选项不会改变原意。 - Andrew T.
137
如果你不想使用数字,也可以使用android:cacheColorHint="@android:color/transparent" - dule
2
@android:color/transparent 可能还有一个额外的好处,即只设置了一个位。存在一个 bug,如果在某些手机上传递 0,则透明度将无法工作。因此,诀窍是设置 1 位。 - Thomas Dignan

62

非常简单,只需在您的布局文件中使用以下行:

android:scrollingCache="false"

像这样:

<ListView 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollingCache="false"
/>

2
它可以工作,但我认为这样做并不明智:https://dev59.com/vmUp5IYBdhLWcg3wCUCx - Morten Holmgaard
1
我曾经遇到过这样一个情况,android:cacheColorHint="#00000000" 无效,但是 android:scrollingCache="false" 却有效。谢谢。 - Yar

27

你可以这样使用:

list.setCacheColorHint(Color.TRANSPARENT);
list.requestFocus(0);

10

android:id="@android:id/list"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:divider="#C8C8C8"
android:background="#C8C8C8"
android:cacheColorHint="#00000000"/>

8
我们有很多选项可以解决这个问题,您可以通过编程将背景设置为透明,例如:
yourlistview.setCacheColorHint(Color.TRANSPARENT); 

或通过xml

android:cacheColorHint="@android:color/transparent"

5
在您的xml中,需要使用Listview设置。
    android:cacheColorHint="@android:color/transparent"

5

我正在listView中使用图片,有时在三星s4上会变黑,甚至不滚动。这是我在适配器中犯的一个愚蠢错误。我只需将我的视图置空即可解决此问题。

 @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            convertView = null; // convert view should be null
            if (convertView == null) {
                holder = new Holder();
                convertView = inflater1.inflate(R.layout.listview_draftreport_item, null);
             } 
        }

4

这个问题有很多答案,但今天我意识到这个问题仍然缺少一个关键信息。

针对这个问题有两种可能的解决方案,都是有效的,但应根据不同情况使用。


方法

当你的ListView有一个纯色背景时,请使用android:cacheColorHint

<item name="android:cacheColorHint">@android:color/transparent</item>

当您的ListView具有(复杂)图像背景时,请使用android:scrollingCache

<item name="android:scrollingCache">false</item>

注意

当你的ListView有固定颜色背景时,这两种方法都能起作用,所以不仅cacheColorHint可以。但是对于固体颜色背景,不建议使用scrolingCache方法,因为它会关闭用于平滑动画和滚动ListView的优化方法。

注意事项:scrolingCache设置为false并不一定意味着ListView的动画和滚动会变慢。


2
android:cacheColorHint="@android:color/transparent"

2
以下方法适用于我:
myListView.setScrollingCacheEnabled(false);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接