滚动时背景ListView变为黑色

TiG*_*Ger 423 android listview

我创建了一个特定的List,它存在于以下元素之外,用于创建一个可滚动的列表,每行包含左侧的Image和右侧的一些文本:

首先是"根"布局:

<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>
Run Code Online (Sandbox Code Playgroud)

然后在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>
Run Code Online (Sandbox Code Playgroud)

只要屏幕静态显示(没有移动)就会正确显示,但是当我开始滚动列表时,行项目的背景(代码中显示的"图标")将是正确显示,但"根"布局的背景将变成完全黑色...当滚动停止时,背景将会,大多数时候,恢复它的颜色...当我测试时我也在TextView该根元素中添加了一个具有相同的背景,当滚动列表时,这个将保留它的颜色...任何想法为什么会发生这种情况,以及如何解决这个问题?

Pra*_*een 771

ListView标签上添加属性

android:cacheColorHint="#00000000" // setting transparent color
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看此博客

  • 或者``android:cacheColorHint ="@ android:color/transparent"`如果你害怕数字:) (135认同)
  • `android:cacheColorHint ="#0000"`如果你害怕零也会有效. (69认同)
  • this.getListView()setCacheColorHint(0).从布局我无法设置颜色.但我设法用上面的代码做到了.我在列表活动的'onCreate'中使用了这段代码.希望这些信息也有帮助. (22认同)
  • @android:颜色/透明可能有一个单位设置的额外好处.有一个错误,如果你在某些手机上传递0透明度不会工作.所以诀窍是设置1位. (2认同)

Mah*_*esh 63

在布局文件中使用此行非常简单:

android:scrollingCache="false"
Run Code Online (Sandbox Code Playgroud)

像这样:

<ListView 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollingCache="false"
/>
Run Code Online (Sandbox Code Playgroud)

  • 它可以工作,但我认为它不聪​​明:http://stackoverflow.com/questions/15570041/scrollingcache (2认同)

nik*_*kki 27

你可以像这样使用:

list.setCacheColorHint(Color.TRANSPARENT);
list.requestFocus(0);
Run Code Online (Sandbox Code Playgroud)


AnD*_*nDx 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"/>
Run Code Online (Sandbox Code Playgroud)


Sam*_*mar 7

我们有很多选择来解决这个问题,你可以通过编程来设置背景透明

yourlistview.setCacheColorHint(Color.TRANSPARENT); 
Run Code Online (Sandbox Code Playgroud)

或通过xml

android:cacheColorHint="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)


jee*_*mar 5

在您的xml中使用Listviewset

    android:cacheColorHint="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)


Ahm*_*lan 5

我在使用图像,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);
             } 
        }
Run Code Online (Sandbox Code Playgroud)