The most commonly used and difficult to use Android control listview

Listview allows users to scroll off screen data into the screen by sliding their fingers up and down, and the original data on the screen will scroll out of the screen

1. Simple usage of listview: first create a listviewtest project, and then modify the activity_ main. XML code

Specify an ID for the listview, and then change the width and height to match_ Parent, so the listview occupies the space of the whole layout

Listview layout

Next, modify the code in mainactivity

The data in the array cannot be directly passed to the listview. We need to use the adapter to complete it. The best one is the arrayadapter, which can specify the data type to be added through generics, and then pass in the data to be adapted in the constructor Notice that we use Android R.layout. simple_ list_ item_ 1 is the ID of the sub item layout of the listview and the data to be adapted

Finally, we need to call the setadapter () method of listview to pass in the constructed adapter object, so that the association between listview and data is established

Listview associated with data

2. Customize the listview interface, then define an entity class as the adaptation type of the listview adapter, create a new class fruit, and prepare a group of pictures

There are only two fields in the fruit class. Name represents the name of the fruit and ImageID represents the resource ID of the corresponding picture of the fruit

Then we need to specify a custom layout for the children of listview, and create a new fruit in the layout directory_ item. xml

In this layout, we define an ImageView to display the picture of fruit and a textview to display the name of fruit

Next, you need to create a custom adapter that inherits from arrayadapter and specifies the generic type as the fruit class Create a new class fruitadapter with the following code:

Fruitadapter overrides a set of constructors of the parent class to pass in the context, ID and data of listview child layout In addition, the getview () method is rewritten. First, the fruit instance of the current item is obtained through the getitem () method, and then the layoutinflater is used to load the layout we passed in for this sub item. Then, the fndviewbyid () method of view is called to obtain the instances of ImageView and textview respectively, and their setimageresource and settext methods are called respectively to set the displayed pictures and text, It is best to return the layout

Modify the code in mainactivity as follows:

As you can see, an initfruits () method is added here to initialize the data of all fruits. The fruit class constructor passes in the name of the fruit and the corresponding picture ID, and then adds the created object to the fruit list. Then we create the fruitadapter object in the oncreate () method and pass the fruitadapter to listview as an adapter

3. Improve the operation efficiency of listview, because the layout is reloaded every time in the getview() method of fruitadapter, which will become an obstacle to performance when the listview scrolls quickly

Therefore, we made a judgment in the getview () method. If the convertview is empty, we will use layoutinflator to load the layout. If it is not empty, we will directly reuse the convertview

Each time in the getview() method, the findviewbyid() method of view will be called to obtain an instance of the control. We can also use a viewholder to optimize the performance of this part and modify the code in the fruitadapter ', as shown below:

We have created an internal class viewholder to cache instances of controls When convertView is empty, create a ViewHolder object and store all instances of the control in ViewHolder, then call View's SetTag () method to store ViewHolder objects in View. When convertview is not empty, call the gettag () method of view to retrieve the viewholder In this way, all instances of the control are cached in the viewholder, so it is unnecessary to obtain the control instance through the findviewbyid () method every time

4. Click event of listview

Final rendering:

5. Summary

First add listview control to the layout, and then customize the adapter. This adapter inherits the initialization data of arrayadapter, passes the data into the custom adapter, and then passes the adapter to listview

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>