ListView of android view component

Source: Internet
Author: User

ListView is a commonly used view component in android systems. Its construction mainly includes two aspects: drawing the UI component and setting the data source. Associate the UI component with the data source through an adapter. Here, the adapter acts as the matchmaker. Before introducing the UI components and data sources, the matchmaker needs to understand both parties, including the layout information of ListItem and the object information of the data source.

There are two commonly used adapters: ArrayAdapter and SimpleAdapter.
Application scenarios of ArrayAdapter:
The ListItem display is single. You only need to display one piece of text.
Example:

For this display mode, the android system provides the default ListItem layout.
Res \ layout \ simple_list_item_1.xml
[Html]
<TextView xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ android: id/text1" android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: textAppearance = "? Android: attr/textAppearanceListItemSmall"
Android: gravity = "center_vertical"
Android: paddingLeft = "? Android: attr/listPreferredItemPaddingLeft"
Android: paddingRight = "? Android: attr/listPreferredItemPaddingRight"
Android: minHeight = "? Android: attr/listPreferredItemHeightSmall "/>
The layout defines a TextView used to display the text content of ListItem. Before constructing an ArrayAdapter, you only need to pass the TextView id and layout File id as parameters to the construction parameters of the ArrayAdapter.
[Java] view plaincopy
New ArrayAdapter <Word> (context, android. R. layout. simple_list_item_1, android. R. id. text1, List <?> DataSource );
The fourth parameter table data source information. The text to be displayed in TextView is the toString () value of the entity element in the List set.

If we want to customize the ListItem layout so that the display of ListView is richer, we often use another adapter SimpieAdapter.
The data source of the SimpieAdapter is structured as follows: List <Map <String, Object>, the List set is no longer an Object, but a Map, Map. the Entry is bound to ListItem, and the Value of Map is mapped to the display content of ListItem.
Suppose we construct the following ListItem layout:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "horizontal">
<ImageView
Android: id = "@ + id/fileItem_image"
Android: layout_width = "32dp"
Android: layout_height = "32dp"
Android: layout_margin = "4dp"
Android: contentDescription = "@ string/img_desc"/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/fileItem_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<TextView
Android: id = "@ + id/fileItem_path"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
</LinearLayout>
The following data source information is provided:
[Java]
List <Map <String, Object> fileList = new ArrayList <Map <String, Object> ();
Map <String, Object> fileInfo = new HashMap <String, Object> ();
FileInfo. put ("img", R. drawable. img );
FileInfo. put ("name", "fileName ");
FileInfo. put ("path", "filePath ");
FileList. add (fileInfo );
You can use the following constructor to build the SimpleAdapter object.
[Java]
New SimpleAdapter (context, fileList, R. layout. filelist_item, new String [] {"img", "name", "path "},
New int [] {R. id. fileItem_image, R. id. fileItem_name, R. id. fileItem_path });
As can be seen from the constructor, the img, name, and path are mapped to the fileItem_image, fileItem_name, and fileItem_path components, respectively, their value values are displayed in the text content of the three components.
Display Effect:

The ListView component can listen to the following events:
Select: setOnItemSelectedListener
Click: setOnItemClickListener
Long Click Event: setOnItemLongClickListener
Long Click Display contextMenu: setOnCreateContextMenuListener

 

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: info-contact@alibabacloud.com 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.