简体   繁体   English

滚动时列表颜色变为黑色

[英]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: 将此添加到xml文件中的listView中:

android:cacheColorHint="#00000000"

or in Java code: 或使用Java代码:

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. 我建议您阅读这篇文章: ListView背景:优化 ,这是自定义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. 如前所述,默认情况下,ListView具有透明/半透明背景,因此Android UI工具箱中的所有默认小部件也是如此。 This implies that when ListView redraws its children, it has to blend the children with the window's background . 这意味着ListView重绘其子级时,必须将子级与窗口的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 . 为了提高滚动操作过程中的绘图性能,Android框架重用了缓存颜色提示 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. 然后,ListView将这些位图直接在屏幕上放大,并且由于已知这些位图是不透明的,因此不需要进行混合。 Also, since the default cache color hint is #191919, you get a dark background behind each item during a scroll. 另外,由于默认的缓存颜色提示为#191919,因此在滚动过程中,每个项目后面都有深色背景。

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. 您可以使用android:cacheColorHint属性从代码(请参见setCacheColorHint(int) ),或者最好从XML进行此操作。 To disable the optimization, simply use the transparent color #00000000 . 要禁用优化,只需使用透明色#00000000 The following screenshot shows a list with android:cacheColorHint="#00000000" set in the XML layout file 以下屏幕快照显示了在XML布局文件中设置了android:cacheColorHint="#00000000"的列表

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? 你可以从这里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. 当您拥有一个填充父视图的视图(通常为surfaceView)时,此技巧可以大大加快2D绘图的速度。 I have see this cause odd background rendering of listViews and scrollViews. 我已经看到这导致listViews和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. 如果您使用null windowbackground hack,则可以尝试为列表视图设置背景色,我敢打赌,滚动问题将会减轻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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