Android listview rolling item background black solution

Source: Internet
Author: User

 

What you can see elsewhere is transferred as a record !!

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 in the list, it is found that all listitems are black, which damages the overall effect, as shown in:

 

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. Therefore, half of the image 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. In the end, we won't see what you didn't want!
Custom split lines of listview

The system control on the Android platform provides flexible custom options. All widgets implemented based on listview or abslistview can set the line spacing through the following methods, the split line can be customized with colors or images.

In listview, we use the property Android: divider = "# ff0000" to define the separator as red. Of course, the value here can point to a drawable image object, if the image height is greater than the system's default pixel, you can set the height by yourself, for example, 6 pixels Android: dividerheight = "6px ", android Development Network prompts that, of course, listview can also be set in Java.

 

1) No background color changes when you click item
Add the following attributes to the listview control in the XML file:
Android: listselector = "@ drawable/timer_list_selector"
Define the attribute value of timer_list_selector in drawable.
Timer_list_selector.xml is defined as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: state_selected = "true" Android: drawable = "@ Android: color/transparent"/>
</Selector>
The transparent defined in colors. XML in the values folder is as follows:
<Color name = "Transparent" >#50000000 </color>

2) set no gap between items
Add the following attributes to the listview control in the XML file:
Android: divider = "#00000000"
Or define it in javacode:
Listview. setdividerheight (0 );

3) Calling the notifydatasetchanged () method in the Custom baseadapter will re-call the getview () method of the baseadapter.

Attentive friends should find that after the background is set in listview. There will be some problems.

 

1. When the listview is dragged, the background image disappears into a black background. After the drag is completed, our own background image will be displayed.

 

2. There is a black shadow on the top and bottom of the listview.

 

3. Set an image as the interval between each item in lsitview.

 

Set the statement in the XML file of listview to solve the above problem.

 

Question 1: complete the following code to solve Android: scrollingcache = "false"

 

Problem 2: Use the following code to solve the problem: Android: fadingedge = "NONE"
Problem 3: Android: divider = "@ drawable/list_driver", where @ drawable/list_driver is an image resource.

 

 

 

Overall:

 

<Listview
Android: Id = "@ + ID/mylistview01"
Android: layout_width = "fill_parent"
Android: layout_height = "287dip"
Android: fadingedge = "NONE"
Android: divider = "@ drawable/list_driver"
Android: scrollingcache = "false"
Android: Background = "@ drawable/List">
</Listview>

Note: QQ technology exchange group: add one if you are interested in 108614806.

 

Related Article

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.