Getting a black screen while scrolling in a ListView on Android

I have one list in which I show the data that I received, it works fine in the emulator and scrolls. But when I test it on the device while scrolling, it shows a black screen and after scrolling stops in the usual readable form. I do not understand why this is happening. I am also attaching a sample code.

XML 1st format: -

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

Second ListView layout: -It also has a scroll view.

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

The code in which I used these 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 a static arraylist "mylist" that has data. Please, if someone knows why he is showing a black screen while scrolling, please help me.

Thanks. lv.setOnItemClickListener (this);

+4
source share
5 answers

I completely agree with all of the above answers, but let me give you this information:

ListView has a transparent / translucent background by default and therefore all widgets are by default in the Android UI toolbox. This means that when the ListView redraws its children, it should mix the children with the background of the window. Once again, this requires costly memory accesses from memory, which are especially painful during scrolling or throwing when drawing occurs dozens of times per second.

To improve drawing performance during scroll operations, the Android framework reuses the cache hint. When this tooltip is given, the structure copies each child of the list to a bitmap filled with the tooltip value (provided that another optimization, called cache scrolling, is not disabled). The ListView then blits these bitmaps directly on the screen and because these bitmaps are known to be opaque, blending is not required. In addition, since the default cache color indicator is # 191919, you get a dark background behind each element while scrolling.

To fix this problem, all you have to do is either disable the hint optimization for the cache color if you are using a non-solid background color, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int) ) or preferably from XML using the android: cacheColorHint attribute. To turn off optimization, just use the transparent color # 00000000. The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file

For More Information: Android ListView Optimization

Decision:

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

+12
source

You should set this on your list:

 android:cacheColorHint="#00000000" 

The goal is to disable the optimization that the framework does to improve drawing performance during scroll operations.

+2
source

Just add the color prompt attribute to the list.

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

Add one property in listview tag in your layout

 android:cacheColorHint="#00000000" 

After that, it should work.

+1
source

Try this code and set the color according to your requirements:

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

+1
source

Source: https://habr.com/ru/post/1384161/


All Articles