Explain the basic use and optimization of listview in android app development with examples

1、 Create a listview directly using the listview component. 1. Create a listview directly in XML and attach an array resource with the entries attribute. The divider attribute is to set the dividing line, which can be divided by color and drawable resources

Define an array resource file arrays.xml under values

2. Create an arrayadapter, specify the list items to be displayed, and associate the adapter in the oncreat method

simple_ list_ item_ 1: List items are plain text

simple_ list_ item_ 2: The list item is ordinary text with a slightly larger font s imple_ list_ item_ Checked: the list item is a selected simple_ list_ item_ multiple_ Choice: list item with check box simple_ list_ item_ single_ Choice: list item with radio button

2、 Let activity inherit listactivity implementation

3、 Use of simpleadapter and baseapter (key points) use of simpleadapter

Add listview component in main.xml, write the layout file items.xml used to layout the contents of list items, and create a simple adapter to associate with listview

3、 The use of baseadapter and the optimized use of listview are the same as above, mainly because the use of baseadapter is more flexible and powerful, and other controls can be added to it.

Note: (many people don't notice that the following statement is findviewbyid under view. Missing view will cause null pointer exception)

The principle of optimization is to dynamically recycle the recycled listview objects (convertview after recycling) to maintain a dynamic balance of the number of listview entries in an interface. (refer to the code Notes for details)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>