简体   繁体   中英

List color turns black while Scrolling

I have one list activity which populates when I click on one tab, the problem is list colour turns black while scrolling it, even though i have not set any attributes to happen like that.. Any suggestions to avoid this problem? Thanks,

Add this to listView in the xml file:

android:cacheColorHint="#00000000"

or in Java code:

getListView().setCacheColorHint(0);

I would suggest you to go through this article: ListView Backgrounds: An Optimization , this is must read article before customizing look of LitsView.

Taken from above document:

As mentioned before, 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

I know its late to answer this question but for others who are facing this problem :

Above techniques may work but the actual thing one should do is adding

android:scrollingCache="false"

in list layout . Like this

     <ListView 
   android:layout_width="30dp"
   android:layout_height="30dp"
   android:id="@+id/myList"
   android:scrollingCache="false"
    />

you can get the reason of this from here scrollingCache?

Thanks

Did you perhaps set a theme on your activity or app that uses the

<item name="android:windowBackground">@null</item>

This is a trick that speeds up 2D drawing quite a bit when you have a view (usually surfaceView) that fills parent. I have see this cause odd background rendering of listViews and scrollViews.

If youre using the null windowbackground hack, you might try setting a background color for your list view and I bet the scrolling issues will abate.

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