Android – ListView, Scrolling Background Color and Java Extra Garbage Collection

Recently worked on implementing listview inside of a viewpager. This was a bit of a challenge but one little bug which turned into another bug.

The first bug was the background always turned black while scrolling which with black text on a white background washed out the text.

After resolving this by setting the background color, the 2nd bug which was very persistent was constant garbage collection while scrolling through the listview. Even if there were only 30-40 items, the garbage collection in logcat seemed awfully high.

After researching these issues I came across the key java commands to help clear them up. There were many helpful posts on stackoverflow.com, but the most helpful I link below.

The Java commands essentially set the listview background to white, matching the rest of the viewpager. The second Java command sets the Cache Color Hint which tells the listview which color to use while scrolling and animating the fade in/out at the top/bottom of the listview.

The commands are:
listView = (ListView) this.findViewById(R.id.listview);
listView.setBackgroundColor(Color.WHITE);
listView.setCacheColorHint(Color.WHITE);

Source: http://stackoverflow.com/questions/3011945/scrolling-a-listview-changes-everything-from-white-to-black/3012134#3012134

Leave a Reply

Your email address will not be published. Required fields are marked *

*