ListView tips: Sliding background, Item gap, etc.

Source: Internet
Author: User

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, 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:


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 is specified as transparent #00000000). Of course, to beautify, you have to 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". Of course, you can set ListView in Java.

No background color changes when you click Item

Add the following attributes to the ListView control in the xml file:

  1. android:listSelector="@drawable/timer_list_selector"  

Define the attribute value of timer_list_selector in drawable.

Timer_list_selector.xml is defined as follows:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <selector xmlns:android=""> 
  3. <item android:state_selected="true" android:drawable="@android :color/transparent" /> 
  4. </selector>  

The transparent defined in colors. xml in the values folder is as follows:

  1. <color name="transparent">#50000000</color>  

Set no gap between items

Add the following attributes to the ListView control in the xml file:

  1. android:divider="#00000000"  

Or define it in javaCode:

  1. listView.setDividerHeight(0);  

Calling the notifyDataSetChanged () method in the Custom BaseAdapter will re-call the getView () method of the BaseAdapter.

After the background is set in listview, some problems will occur:

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.


  1. <ListView 
  2. android:id="@+id/myListView01" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="287dip" 
  5. android:fadingEdge="none"  
  6. android:scrollingCache="false" 
  7. android:divider="@drawable/list_driver" 
  8. android:background="@drawable/list"> 
  9. </ListView> 

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: 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.