简体   繁体   English

滚动时背景 ListView 变黑

[英]Background ListView becomes black when scrolling

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具有相同背景的元素,当列表滚动时,这个元素会保留它的颜色......知道为什么会发生这种情况,以及如何解决这个问题?

Add an attribute on the ListView TagListView标签上添加一个属性

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

For more details check this blog有关更多详细信息,请查看此博客

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

you can use like this:你可以这样使用:

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

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

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"

在您的 xml 中在哪里使用Listview

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

I am using images in listView and it turns black sometimes in samsung s4 not even scrolling.我在listView使用图像,它有时在 samsung s4 中变黑,甚至不滚动。 It was a stupid mistake which I have done in the adapter.I just put my view to null to fix this issue这是我在适配器中犯的一个愚蠢的错误。我只是将我的视图设置为 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);
             } 
        }

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.当您的ListView具有纯色背景时,请使用android:cacheColorHint

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

Use android:scrollingCache when your ListView has a (complex) image as background.当您的ListView具有(复杂)图像作为背景时,请使用android: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.当您的ListView具有纯色背景时,两种方法都可以使用,因此不仅cacheColorHint可以使用。 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.但是不建议将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的动画和滚动会变慢。

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

以下对我有用:

myListView.setScrollingCacheEnabled(false);

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

or或者

don't set background of listview .不要设置listview背景。

添加属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM