简体   繁体   English

在Android中的ListView中滚动时获取黑屏

[英]Getting Black Screen while Scrolling in ListView in Android

I have one listview in which i am displaying data that i got, it run fine in emulator and scroll also. 我有一个listview,我在其中显示我得到的数据,它在模拟器中运行良好并滚动。 But when i test it on device at the time of scrolling it is showing black screen and after scroll stops it is in the normal readable form. 但是当我在滚动时在设备上测试它时显示黑屏,滚动停止后它是正常的可读形式。 I don't why this is happening. 我不知道为什么会这样。 I am also attaching sample code. 我还附上了示例代码。

1st XML Layout :- 第一个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" />

2nd ListView Layout :-It has scroll view also. 第二个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" />

Code where i have used this layouts :- 我使用过此布局的代码: -

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

I have static arraylist "mylist" which has the data. 我有静态arraylist“mylist”,它有数据。 Please if anyone has any idea that why it shows black screen at the time of scrolling please guide me. 如果有人知道为什么它在滚动时显示黑屏请指导我。

Thank you lv.setOnItemClickListener(this); 谢谢lv.setOnItemClickListener(this);

I am totally agree with all the answers listed above, but let me give you this information: 我完全同意上面列出的所有答案,但是让我告诉你这些信息:

ListView has a transparent/translucent background by default, and so all default widgets in the Android UI toolkit. 默认情况下,ListView具有透明/半透明背景,因此Android UI工具包中的所有默认小部件都是如此。 This implies that when ListView redraws its children, it has to blend the children with the window's background. 这意味着当ListView重绘其子项时,它必须将子项与窗口的背景混合。 Once again, this requires costly readbacks from memory that are particularly painful during a scroll or a fling when drawing happens dozen of times per second. 再一次,这需要从内存中进行昂贵的回读,这在滚动或拖动期间特别痛苦,因为每秒进行数十次绘制。

To improve drawing performance during scrolling operations, the Android framework reuses the cache color hint. 为了提高滚动操作期间的绘图性能,Android框架重用了缓存颜色提示。 When this hint is set, the framework copies each child of the list in a Bitmap filled with the hint value (assuming that another optimization, called scrolling cache, is not turned off). 设置此提示后,框架会在填充了提示值的位图中复制列表的每个子项(假设另一个优化,称为滚动缓存,未关闭)。 ListView then blits these bitmaps directly on screen and because these bitmaps are known to be opaque, no blending is required. 然后ListView直接在屏幕上显示这些位图,因为已知这些位图是不透明的,所以不需要混合。 Also, since the default cache color hint is #191919, you get a dark background behind each item during a scroll. 此外,由于默认缓存颜色提示为#191919,因此在滚动期间每个项目后面都会显示深色背景。

To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. 要解决此问题,您所要做的就是禁用缓存颜色提示优化,如果使用非纯色背景,或将提示设置为适当的纯色值。 You can do this from code (see setCacheColorHint(int) ) or preferably from XML, by using the android:cacheColorHint attribute. 您可以使用android:cacheColorHint属性从代码(请参阅setCacheColorHint(int) )或最好从XML执行此操作。 To disable the optimization, simply use the transparent color #00000000. 要禁用优化,只需使用透明色#00000000。 The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file 以下屏幕截图显示了在XML布局文件中设置android:cacheColorHint="#00000000"的列表

For more info: Android ListView Optimization 有关更多信息: Android ListView优化

Solution: 解:

You can either use android:cacheColorHint="@android:color/transparent" or android:cacheColorHint="#00000000" 您可以使用android:cacheColorHint="@android:color/transparent"android:cacheColorHint="#00000000"

You have to set this in your listview: 你必须在listview中设置它:

android:cacheColorHint="#00000000"

The goal is to disable an optimization that the framework does for improving drawing performance during scrolling operations. 目标是禁用框架在滚动操作期间为提高绘图性能所做的优化。

Try this code and set colour according to your requirement: 尝试此代码并根据您的要求设置颜色:

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

Just add color hint attribute to list view. 只需将颜色提示属性添加到列表视图即可。

            <ListView android:layout_height="wrap_content"
                android:layout_width="match_parent" android:cacheColorHint="@android:color/transparent" />

Add one property in listview tag in your layout 在布局中的listview标记中添加一个属性

android:cacheColorHint="#00000000"

After that it should work. 之后它应该工作。

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

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