Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 838
Scorpion Avatar asked Dec 04 '22 19:12

Scorpion


1 Answers

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. This implies that when ListView redraws its children, it has to blend the children with the window's background. 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. 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. Also, since the default cache color hint is #191919, you get a dark background behind each item during a scroll.

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. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file

For more info: Android ListView Optimization

Solution:

You can either use android:cacheColorHint="@android:color/transparent" or android:cacheColorHint="#00000000"

like image 150
Paresh Mayani Avatar answered Dec 28 '22 09:12

Paresh Mayani