Why is the listview item black?

Source: Internet
Author: User

I encountered a problem during development today. My listview has a background image, but when I click the blank area below the listview, the item background in the listview turns black.

I thought it was a bug in Android, But I searched on Google and found the answer in the following article.

Http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html

This technique is used throughout the system to indicate that the container can be scrolled.

This rendering issue is caused by an optimization of the android framework enabled by default on all instances of listview.

However, the solution is also proposed. Setting the XML Attribute of listview can solve the problem.
Android: cachecolorhint = "#00000000"

Of course, the author of the original article also admitted that this practice has brought inconvenience to developers.

 

In Android, listview is the most commonly used control. During the uidesign, many people want to change its background so that it can conform to the overall uidesign, it's easy to change the background. You only need to prepare an image and specify the property Android: Background = "@ drawable/BG". But don't be so happy. When you do this, you will find that the background has changed, however, when you drag or click the blank position of the list, it is found that all listitems are black, which damages the overall effect.

Why?

**************************************** **************************************** ******



Starting from the effect of listview, the default listitem background is transparent, while the background of listview is fixed, therefore, if the display content of each item is mixed with the background in real time during scroll of the scroll bar, the android system uses android to optimize this process: the default color value of cachecolorhint in the black topic is #191919, so the image is displayed, half of which is black.


What should we do?

        If you only change the background color, you can directly specify the Android: cachecolorhint as the color you want. If you use an image as the background, you only need to change the Android: cachecolorhint can be specified as transparent (#00000000). Of course, to beautify the image, you must sacrifice some efficiency.

**************************************** **************************************** **************************************** **********************************

Recently, listview shows some string data. If the invention is not to hold down the text project group and drag the listview, everything is safe. However, if you hold down the text project group, all the text project teams will become black.
The solution is actually very simple. The root cause of the problem is the cache color. There are two methods:

1. In the layout XML file
In listview, add the partial attribute.

Android: cachecolorhint = "#00000000 ″

2. In the code, apply the setcachecolorhint method to listview. // Http://www.my400800.cn

 

1 LV. setcachecolorhint (color. Transparent );

 

Pay attention to this. When you apply code, you cannot apply LV. setbackgroundcolor (color. Transparent) to make LV transparent. Otherwise, there will be no results.

**************************************** **************************************** **************************************** ***********************************

Android provides multiple implementation methods for listview to make the displayed content more beautiful through custom display layout, but we will find such a problem. When we operate listview, if you do not use the upper and lower buttons on your phone, you can drag the view by touch to find that the background of listview is black and all selected text becomes dark.

In fact, this problem occurs because the listview has a cache color mechanism, so we can solve this problem by setting the cache color to transparent.

A. Set by layout attributes (directly defined in the attributes of listview)

Android: cachecolorhint = "#00000000 ″

B. Set it directly in the code

Listview. setcachecolorhint (color. Transparent );

In this way, the black problem can be solved.

**************************************** **************************************** **************************************** ***********

Listview is a commonly used display control. The default background is the same transparent color as the system window. If a background image or background color is added to listview, The listview will be black when scrolling, because, during scrolling, when the view in the list is re-painted, the system still uses the default transparent color. The color value is # ff191919. to change this situation, you only need to call setcachecolorhint (0) of listview and set the color value to 0 or the attribute Android: cachecolorhint = "#00000000" of listview in XML files. When scrolling, there will be no background color when re-painting the view.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.