简体   繁体   中英

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

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"

You have to set this in your 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

android:cacheColorHint="#00000000"

After that it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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