在Android中滚动ListView时出现黑屏

4

我有一个列表视图,在其中显示我获取的数据,它在模拟器上运行良好并且可以滚动。但是当我在设备上测试时,在滚动时它会显示黑屏,并且停止滚动后才能以正常可读形式显示。我不知道为什么会发生这种情况。我也附上了示例代码。

第一个XML布局:

<?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="fill_parent"
    android:orientation="vertical"
    android:padding="7dp" >

<TextView
    android:id="@+id/item_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:textColor="#000000"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="20dp" />

<TextView
    android:id="@+id/item_subtitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:padding="2dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="13dp" />

第二个ListView布局:它还带有滚动视图。

<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="#FFFFFF">   

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="No data"
    android:textColor="#000000" />

我使用这些布局的代码如下:


ListAdapter adapter = new SimpleAdapter(this, HomeMenuActivity.mylist , R.layout.listdata, 
            new String[] { "Name", "Vicinity" }, 
            new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    lv = getListView();
    lv.setTextFilterEnabled(true);

我有一个静态数组列表"mylist",其中包含数据。如果有人知道为什么在滚动时会出现黑屏,请指导我。
谢谢。 lv.setOnItemClickListener(this);
5个回答

11

我完全同意以上列出的所有答案,但是让我给你提供一些信息:

ListView默认具有透明/半透明背景,以及安卓UI工具包中的所有默认小部件。这意味着当ListView重绘其子项时,它必须将子项与窗口的背景混合。再次说明,在滚动或快速滑动期间,由于绘制每秒进行数十次,因此这需要从内存中读取昂贵的内容,这会特别痛苦。

为了改善滚动操作期间的绘图性能,安卓框架重复使用缓存颜色提示。设置此提示时,框架会将列表的每个子项复制到填充有提示值的位图中(假设未关闭另一种优化,称为滚动缓存)。然后,ListView直接在屏幕上平移这些位图,并且由于已知这些位图是不透明的,因此无需混合。此外,由于默认缓存颜色提示为#191919,在滚动期间每个项目后面都会得到一个黑色背景。

要解决此问题,您只需禁用缓存颜色提示优化(如果使用非纯色背景)或将提示设置为适当的纯色值即可。您可以从代码中执行此操作(请参见setCacheColorHint(int)),或者更好地从XML中执行该操作,方法是使用android:cacheColorHint属性。要禁用优化,只需使用透明颜色#00000000即可。下面的屏幕截图显示了在XML布局文件中设置了android:cacheColorHint="#00000000"的列表

更多信息请参见:安卓ListView优化

解决方案:

您可以使用android:cacheColorHint="@android:color/transparent"android:cacheColorHint="#00000000"


1
非常感谢Paresh提供详细信息,再次感谢您的帮助。 - Scorpion
很棒的回答!非常有用并且详细 ;D - Felipe Conde
1
优秀的、技术丰富的回答。对于开发人员、学生、研究人员等非常有用。 - SIVAKUMAR.J

2

您需要在列表视图中进行设置:

android:cacheColorHint="#00000000"

目标是禁用框架在滚动操作期间提高绘图性能的优化。


1
只需在列表视图中添加颜色提示属性即可。
            <ListView android:layout_height="wrap_content"
                android:layout_width="match_parent" android:cacheColorHint="@android:color/transparent" />

1
在你的布局中,为listview标记添加一个属性。
android:cacheColorHint="#00000000"

之后它应该可以工作。


1

尝试使用此代码并根据您的需求设置颜色:

String arr[]=new String[]{"a","bv"};
ListView lv;    

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    lv=(ListView)findViewById(R.id.lv1);
    ArrayAdapterad=newArrayAdapter(getApplicationContext(),R.layout.check_it,R.id.txt_color,ar);
    lv.setAdapter(ad);
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<ListView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:id="@+id/lv1"
    android:background="#000"
    android:scrollingCache="false"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#fff"
    android:id="@+id/txt_color"/>


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