[Wheat College] Android development tutorial custom ListView background, androidlistview
Android custom ListView background
Android ListView is a very commonly used control in Android development. During the uidesign, many people want to change its background to achieve better visual effects. changing its background is actually very simple, we only need to prepare an image and specify the property android: background = "@ drawable/bg", but don't be happy too early. When you do this, you will find that the background is changed, however, when you drag or click the blank position of the list, it is found that all listitems are black, 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, it will sacrifice a little efficiency to beautify. Final beautification Effect
How does android listView specify the background settings of an item?
To solve this problem, you can refresh the adapter. First, you can customize the adapter class, define a mySelection attribute in the adapter class, and define a setMySelection (int index) {mySelection = index; notifyDataSetChanged ();} method. In the getView method, determine whether the index of the current returned view is equal to mySelection. If the value is equal, set a special background. The method is as follows: Call the setMySelection method where the adapter is used. Do not forget to pass the index value of the background option you want to change.
Pay attention to yydatasetchanged () in the setMySelection method. It is very important to help you.
Android listview configures a record with a background image
No. You use the GETVIEW in baseadapter in list. setadapter.
You need to set the number of rows before setting the graph ~ To be set in getcount
After you set the number of rows, call an image group with the same length as the number of rows you set in getview ~ For example:
You put 10 images in the drawable in the project, and create an array ~ Obtain the IDs of the 10 images ~ Then
Use the image [position] // image array in GETVIEW to represent the array of your saved image ID.
The obtained ID is the ID of the current row ~