滚动时,背景列表视图变为黑色

时间:2022-12-29 16:58:37

I have created a specific List which exists out of the following elements to create a scrollable list with every row containing a Image on the left side and some text on the right side:

我已经创建了一个特定的列表,它存在于下面的元素中,创建了一个可滚动的列表,每一行都包含一个图像在左边,一些文本在右边:

To begin with a "root" layout :

以“根”布局开始:

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

and then within the ListView I place the following "row" item :

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

<?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>

As long as the screen is shown statically (as in no movement) it will be shown correctly, but when I start scrolling through the list the background of the row-item (an "icon" as can be shown in the code) will be shown correctly but the background of the "root" layout will become completely black... when the scrolling stops the background will, most of the times, get back its color... As I test I also added a TextView in that root-element with the same background, this one will detain it's color when the List is scrolled... Any idea why this is happening, and how to solve this?

只要屏幕显示静态(如没有运动),它将显示正确,但是当我开始滚动列表行条目的背景(一个“图标”可以显示在代码)将显示正确,但“根”布局的背景将成为全黑……当滚动停止时,大部分时间,返回它的颜色…在测试时,我还在根元素中添加了一个TextView,它的背景是相同的。你知道为什么会这样,怎么解决吗?

11 个解决方案

#1


766  

Add an attribute on the ListView Tag

在ListView标记上添加一个属性

android:cacheColorHint="#00000000" // setting transparent color

For more details check this blog

欲了解更多详情,请浏览本博客

#2


61  

It's very simple just use this line in your layout file :

在你的布局文件中使用这一行很简单:

android:scrollingCache="false"

like this:

是这样的:

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

#3


25  

you can use like this:

你可以这样使用:

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

#4


8  

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

#5


6  

We have plenty of options for this problem, you can set the background as transparent through programming like

对于这个问题,我们有很多选择,您可以通过类似的编程将背景设置为透明的

yourlistview.setCacheColorHint(Color.TRANSPARENT); 

or through xml

或通过xml

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

#6


4  

I am using images in listView and it turns black sometimes in samsung s4 not even scrolling. It was a stupid mistake which I have done in the adapter.I just put my view to null to fix this issue

我使用的是listView中的图像,在三星s4中有时会变成黑色,甚至不会滚动。我在适配器上犯了一个愚蠢的错误。我只是将我的视图设置为null来解决这个问题。

 @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);
             } 
        }

#7


3  

There are allot of answers to this question but today I realized this question is still missing a critical piece of information.

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

There are two possible solutions for the problem, both work but each should be used in different situations.

对于这个问题有两种可能的解决方案,两种都可以,但是每种都应该在不同的情况下使用。


Methods

方法

Use android:cacheColorHint when your ListView has a solid color background.

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

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

Use android:scrollingCache when your ListView has a (complex) image as background.

使用android:当您的ListView有一个(复杂的)图像作为背景时,可以使用scrollingCache。

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

Note

When your ListView has a solid color background both methods will work, so not only the cacheColorHint will work. But it's not recommended to use the scrolingCache method for solid color backgrounds since it turns off an optimization method used for smooth animating and scrolling the ListView.

当您的ListView有一个纯色背景时,这两个方法都可以工作,因此不仅cacheColorHint可以工作。但不建议使用scrolingCache方法来实现纯色背景,因为它关闭了用于平滑动画和滚动ListView的优化方法。

Note for the note: scrolingCache set to false does not necessarily mean the ListView's animations and scrolling will become slow.

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

#8


3  

In your xml where to use Listview set

在您的xml中使用Listview集

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

#9


1  

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

#10


1  

The following worked for me:

以下是我的工作经验:

myListView.setScrollingCacheEnabled(false);

#11


0  

android:cacheColorHint="#00000000"// setting transparent color

android:cacheColorHint = " # 00000000 " / /设置透明色

or

don't set background of listview.

不要设置listview的背景。

#1


766  

Add an attribute on the ListView Tag

在ListView标记上添加一个属性

android:cacheColorHint="#00000000" // setting transparent color

For more details check this blog

欲了解更多详情,请浏览本博客

#2


61  

It's very simple just use this line in your layout file :

在你的布局文件中使用这一行很简单:

android:scrollingCache="false"

like this:

是这样的:

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

#3


25  

you can use like this:

你可以这样使用:

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

#4


8  

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

#5


6  

We have plenty of options for this problem, you can set the background as transparent through programming like

对于这个问题,我们有很多选择,您可以通过类似的编程将背景设置为透明的

yourlistview.setCacheColorHint(Color.TRANSPARENT); 

or through xml

或通过xml

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

#6


4  

I am using images in listView and it turns black sometimes in samsung s4 not even scrolling. It was a stupid mistake which I have done in the adapter.I just put my view to null to fix this issue

我使用的是listView中的图像,在三星s4中有时会变成黑色,甚至不会滚动。我在适配器上犯了一个愚蠢的错误。我只是将我的视图设置为null来解决这个问题。

 @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);
             } 
        }

#7


3  

There are allot of answers to this question but today I realized this question is still missing a critical piece of information.

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

There are two possible solutions for the problem, both work but each should be used in different situations.

对于这个问题有两种可能的解决方案,两种都可以,但是每种都应该在不同的情况下使用。


Methods

方法

Use android:cacheColorHint when your ListView has a solid color background.

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

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

Use android:scrollingCache when your ListView has a (complex) image as background.

使用android:当您的ListView有一个(复杂的)图像作为背景时,可以使用scrollingCache。

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

Note

When your ListView has a solid color background both methods will work, so not only the cacheColorHint will work. But it's not recommended to use the scrolingCache method for solid color backgrounds since it turns off an optimization method used for smooth animating and scrolling the ListView.

当您的ListView有一个纯色背景时,这两个方法都可以工作,因此不仅cacheColorHint可以工作。但不建议使用scrolingCache方法来实现纯色背景,因为它关闭了用于平滑动画和滚动ListView的优化方法。

Note for the note: scrolingCache set to false does not necessarily mean the ListView's animations and scrolling will become slow.

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

#8


3  

In your xml where to use Listview set

在您的xml中使用Listview集

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

#9


1  

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

#10


1  

The following worked for me:

以下是我的工作经验:

myListView.setScrollingCacheEnabled(false);

#11


0  

android:cacheColorHint="#00000000"// setting transparent color

android:cacheColorHint = " # 00000000 " / /设置透明色

or

don't set background of listview.

不要设置listview的背景。