Common usage analysis of listview in Android programming development

This article describes the common usage of listview in Android programming. Share with you for your reference, as follows:

1、 Steps for using listview

The use of listview usually has the following three elements:

(1) Layout of each item in listview; (2) Fill in the contents in the listview; (3) An adapter that integrates content with pages

Therefore, there are usually three steps to use listview:

(1) Create a layout file for listview entries (or use the layout provided by the Android SDK); (2) Create and fill the contents in the listview, such as strings, pictures, etc; (3) Create an adapter and bind it to listview

2、 Create a listview using the default layout file

This example uses the default layout file of the Android SDK: android.r.layout.simple_ list_ item_ 1, so just create the main activity layout file.

ListViewTest.xml

Next is the activity file.

ListViewTestActivity.java

The operation result is:

3、 Listview supporting multiple selection

Android also provides an item layout file that supports multiple choices: Android. R. layout. Simple_ list_ item_ multiple_ choice.

But you must also call the listview. Setchoicemode() method.

The modified code snippet is as follows:

The operation result is:

4、 Respond to touch events

In order for items in the listview to respond to click events, you need to create an onitemclicklistener class and bind it to the listview. Without much nonsense, the following are activity class files:

The operation result is:

In addition to onitemclicklistener, there are onitemlongclicklistener, onitemselectedlistener and other listeners

I hope this article will help you in Android programming.

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