Android Study Notes (17th): Go back to listview

Source: Internet
Author: User

Listview is often used because of the cell phone screen size and the touch screen features of fingers. In Android learning notes (11): Activity-listview, each entry in the list has only one data and only one view. In this tutorial, we will learn further changes, to make the list more vivid, you only need to further describe apdater.

Example 1: Each element has an icon and an information data

1) set the XML file on the main interface

<Linearlayout...>
<! -- We need to perform the local function on the List entry, so the ID uses "@ Android: ID/List" -->
<Listview Android: Id ="@ Android: ID/List"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</Linearlayout>

2) set the entry. XML format for each element

<Linearlayout...>
<! -- In each element (in each row list), there is an image icon, followed by our data information -->
<ImageviewAndroid: Id = "@ + ID/c82_icon"
Android: layout_width = "44px"
Android: paddingleft = "2px"
Android: paddingright = "2px"
Android: paddingtop = "10px"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/android_normal"/>
<TextviewAndroid: Id = "@ + ID/c82_label"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textsize = "44sp"/>
</Linearlayout>

3) source code

The source code is very simple. For details, refer to the first example in activity-listview in Android Study Notes (11. The difference is the method for setting the adapter. The original example uses the UI format Android provided by Android. r. layout. simple_list_item_1. In this example, we will use the custom Layout format to describe the elements in the list addition:

Public class chapter8test2 extends listactivity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. chapter_8_test2 );
/* The first parameter is context, the last parameter is the data information source item, and the second parameter is the layout XML file describing the entry, the third parameter is the widget in layout, the corresponding element of the data source. */
Setlistadapter (New arrayadapter <string> (this, R. layout. Entry, R. Id. c82_label, chapter8.items ));
}

Public voidOnlistitemclick(Listview parent, view, int position, long ID ){
Toast. maketext (getapplicationcontext (), chapter8.items [position], Toast. length_short). Show ();

}

Example 2: dynamically set each element based on the layout XML file

The above method can be used in a simple way, but the following two cases

  • Not all units use the same layout
  • Widgets that need to be set in the list unit, for example, using different icons

We need to create a subclass of the adapter and overwrite getview () to describe our unit style. In the following example, we modify the first example and use different icons for words with different lengths.

Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. chapter_8_test2 );
Setlistadapter(NewIconicadapter());
}

Private classIconicadapterExtends arrayadapter <string> {
/* Step 1: Write the constructor. For the first example, fill in the layout XML file and data source to complete the necessary initialization work */
Iconicadapter (){
Super(Chapter8test3. This, R. layout. Entry, chapter8.items );
}

/* Step 2: rewrite getview () to describe the format of each element. In the input, position indicates the sequential position of the list, the returned view is even if the list is rendered by the element at position */
Public ViewGetview(INT position, view convertview, viewgroup parent ){
/* Step 2.1: According to the layout XML file, use layoutinflater to film a view object as the basis of the UI of the list element */
// The layoutinflater class is used to instantiate the layout XML file as a corresponding view object. It is never used directly, but uses getlayoutinflater () or getsystemservice (string) to obtain the layoutinflater instance that has been attached to the current context standard. For example, layoutinflater Inflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );
Layoutinflater infalter =Getlayoutinflater();
// Obtain the relevant XML structure from the first parameter, and the second parameter is viewgroup root. The last parameter indicates whether to throw inflateexception if an error occurs.
View row = infalter.Inflate (R. layout. Entry, parent,
False
)
;

/* Step 2.2: set the format and information of each widget in each view. In this example, if the information length is greater than 4, an icon is displayed, show another icon if it is less than or equal */
Textview label = (textview)Row. findviewbyid(R. Id. c82_label );
Imageview icon = (imageview)Row. findviewbyid(R. Id. c82_icon );

Label. settext (chapter8.items [position]);
If (chapter8.items [position]. Length ()> 4 ){
Icon.Setimageresource(R. drawable. android_focused );
} Else {
Icon. setimageresource (R. drawable. android_normal );
}
/* Step 2.3, return the view at the position */

Return row;
}
}

Example 3: make the program more efficient

In Example 2, you can set the UI of each list element flexibly. However, in terms of efficiency, You need to obtain the view based on getview () for each slide, it will slow down, and the cell phone battery will be consumed each time the CPU is calculated. In getview (), one parameter is view convertview. When we first give the UI view of this element, convertview is zero, convertview is the previously created view object (reuse, and avoid using Java to consume CPU [Power Consumption] During garbage collection). You can reuse the previously created view. If you need to change the content, you can use findviewbyid to obtain the corresponding widget object and set the content. In this way, the UI only refreshes the set part. The following is how to modify the getview in Example 3.

/* To make the program more efficient, use the previously created view (that is, the second parameter) to create a new view only when this element is displayed for the first time */
Public View getview (INT position, view convertview, viewgroup parent ){
View row = convertview;
// In this example, you can assign a value in the first setting because the widget content remains unchanged. In this example, we dynamically display the content (the changes need to be handled) and reset the content each time.
If (ROW = NULL ){
Layoutinflater Inflater = getlayoutinflater ();
Row = Inflater. Inflate (R. layout. chapter_8_test2_entry, parent, false );
}
Textview label = (textview) Row. findviewbyid (R. Id. c82_label );
Label. settext (chapter8.items [position]);

Imageview icon = (imageview) Row. findviewbyid (R. Id. c82_icon );
Label. settext (chapter8.items [position]);
If (chapter8.items [position]. Length ()> 4 ){
Icon. setimageresource (R. drawable. android_focused );
} Else {
Icon. setimageresource (R. drawable. android_normal );
}

Return row;
}

Example 4: make the program more efficient settag ()

In the above example, we reuse the created view, but the findviewbyid needs to be searched in the view level. If the view structure is complex, it also consumes CPU. In addition, for each list element, in the actual program, the list element may also store some data. In this example, we introduce the settag () and gettag () methods. Each view can be bound with an object through settag (). You can use gettag () to retrieve this object. This avoids the layer-by-layer query of the widget by the findviewbyid and achieves fast positioning.

// Step 1: Set a class to store the information of the list element.
Class viewwrapper {
View base;
Textview label = NULL;
Imageview icon = NULL;

Viewwrapper (view base ){
This. base = base;
}

Textview getlabel (){
If (Label = NULL)
Label = (textview) base. findviewbyid (R. Id. c82_label );
Return label;
}

Imageview getIcon (){
If (icon = NULL)
Icon = (imageview) base. findviewbyid (R. Id. c82_icon );
Return icon;
}
}

... The remaining content is the same as Example 2 and Example 3. We rewrite the getview () of the iconicadapter ()

Public View getview (INT position, view convertview, viewgroup parent ){
View row = convertview;
Viewwrapper wrapper = NULL;
// Step 2: If no view is created, create the view and bind the viewwrapper object with settag (). If the view object has been created, use gettag () to obtain the viewwrapper object.
If (ROW = NULL ){
Layoutinflater Inflater = getlayoutinflater ();
Row = Inflater. Inflate (R. layout. chapter_8_test2_entry, parent, false );
Wrapper = new viewwrapper (ROW );
Row.Settag(Wrapper );
} Else {
Wrapper = (viewwrapper) Row.Gettag();
}

Wrapper. getlabel (). settext (chapter8.items [position]);

If (chapter8.items [position]. Length ()> 4 ){
Wrapper. getIcon (). setimageresource (R. drawable. android_focused );
} Else {
Wrapper. getIcon (). setimageresource (R. drawable. android_normal );
}

Return row;
}
}

Related Links: My andriod development articles

Related Article

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.