Use Case of listview paging (with images) display

Source: Internet
Author: User

Listview is the most commonly used list type control in Android. In listview, many style items include plain text and images. Its Inheritance relationships are as follows:
Java. Lang. Object
Using Android. View. View
Using Android. View. viewgroup
Using Android. widget. adapterview <t extends Android. widget. Adapter>
Using Android. widget. abslistview
Using Android. widget. listview
Android. widget. listview inherits Android. View. viewgroup.
First, let's look at a text-only listview example. After the case is run, a city list is shown in 6-8. Select a city and a toast is displayed, the concepts and usage of toast are described in the next section.


Figure 6-8 listview
For program code, see code list 6-4:
[Code list 6-4] chapter6_3/src/COM/work/listview_1_activity.java
Public class listview_1_activity extends activity {
Private listview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. listview_activity );

Listview = (listview) findviewbyid (R. Id. listview01 );
Arrayadapter <string> adapter = new arrayadapter <string> (this,
Android. R. layout. simple_list_item_1, mstrings );
Listview. setadapter (adapter );
Listview. setonitemclicklistener (New adapterview. onitemclicklistener (){
@ Override
Public void onitemclick (adapterview <?> Parent, view V, int POs,
Long ID ){
Toast. maketext (listview_javasactivity.this, mstrings [POS],
Toast. length_short). Show ();
}
});
}

Private string [] mstrings = {
"Beijing", "Tianjin", "Shanghai", "Chongqing", "Urumqi ",...};
}
You should be familiar with arrayadapter, Where Android. R. layout. simple_list_item_1 uses the layout style of the system. The Android system provides many such layout files, but some are suitable for the listview control, some are suitable for the spinner control, and some are suitable for its list control. This requires attention during use.

In this way, you need to add the listview control to the layout file listview_activity.xml:
<Listview Android: Id = "@ + ID/listview01" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"> </listview>
Listview is a list-type control that is commonly used in Android. When multiple pieces of information need to be displayed, you can use listview for display. It is precisely because listview is widely used, therefore, Android provides a list-type activity-listactivity to simplify listview development.

A simple listview function is implemented by inheriting the listactivity class, instead of directly using the listview control. In the same case, if listactivity is used, refer to the code listing 6-5:

[Code list 6-5] chapter6_3/src/COM/work/listview_1.java
Public class listview_1 extends listactivity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

Setlistadapter (New arrayadapter <string> (this,
Android. R. layout. simple_list_item_1, mstrings ));
Getlistview (). setonitemclicklistener (New adapterview. onitemclicklistener (){

@ Override
Public void onitemclick (adapterview <?> Parent, view V, int POs,
Long ID ){
Toast. maketext (listview_1.this, mstrings [POS],
Toast. length_short). Show ();
}
});
}

Private string [] mstrings = {
"Beijing", "Tianjin", "Shanghai", "Chongqing", "Urumqi ",...};
}
You can see that no layout file is used here, which means you do not need to use the r file to obtain the control. Therefore, the getlistview () method is used in the program to obtain the listview control. There are two ways to process the project Click Event of listview. One is to set the setonitemclicklistener method with the listview object. The Code is as follows:

Getlistview (). setonitemclicklistener (New adapterview. onitemclicklistener (){
@ Override
Public void onitemclick (adapterview <?> Parent, view V, int POs,
Long ID ){
Toast. maketext (listview_1.this, mstrings [POS],
Toast. length_short). Show ();
}
});
The other method is to override the onlistitemclick (listview L, view V, int position, long ID) method of listactivity. The Code is as follows.

@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
Toast. maketext (listview_1.this, mstrings [position], Toast. length_short)
. Show ();
}
Let's look at the example of a custom Adapter. This is a listview with icons. The running result is 6-9.

Figure 6-9 custom Adapter
For the relevant program code, see the code list 6-6:
[Code list 6-6] chapter6_3/src/COM/work/listviewicon_3.java
Public class listviewicon_3 extends listactivity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setlistadapter (New efficientadapter (this ));
}

Private Static final string [] DATA = {
"Beijing", "Tianjin", "Shanghai", "Chongqing", "Harbin ",
"Shijiazhuang", "Qinhuangdao", "Jinan", "Qingdao", "Nanjing ",
"Sanya", "Kunming", "Chengdu", "Changsha", "Wuhan ",
"Jiujiang", "Hong Kong", "Macao", "Lanzhou", "Zhangjiakou "};
...
}
The custom adapter is efficientadapter. For more information about the code of efficientadapter, see code listing 6-7:
[Code list 6-7] chapter6_3/src/COM/work/listviewicon_3.java
Private Static class efficientadapter extends baseadapter {
Private layoutinflater minflater;
Private bitmap micon0;
Private bitmap micon1;
... ...
Public efficientadapter (context ){
Minflater = layoutinflater. From (context );
Micon0 = bitmapfactory. decoderesource (context. getresources (), R. drawable. noicon );

Micon1 = bitmapfactory. decoderesource (context. getresources (), R. drawable. Beijing );

... ...
}

Public int getcount (){
Return data. length;
}

Public object getitem (INT position ){
Return data [position];
}

Public long getitemid (INT position ){
Return position;
}

Public View getview (INT position, view convertview, viewgroup parent ){
Viewholder holder;

If (convertview = NULL ){
Convertview = minflater. Inflate (R. layout. Main, null );

Holder = new viewholder ();
Holder. Text = (textview) convertview. findviewbyid (R. Id. textview );

Holder. Icon = (imageview) convertview. findviewbyid (R. Id. Icon );

Convertview. settag (holder );
} Else {
Holder = (viewholder) convertview. gettag ();
}

Holder. Text. settext (data [position]);
Switch (position)
{
Case 0:
Holder. Icon. setimagebitmap (micon1 );
Break;
Case 1:
Holder. Icon. setimagebitmap (micon2 );
Break;
...
Default:
Holder. Icon. setimagebitmap (micon0 );
Break;
}

Return convertview;
}

Static class viewholder {
Textview text;
Imageview icon;
}
}
You can write a custom adapter to inherit the baseadapter class. If you use a database, you can inherit the cursoradapter class. In this example, the baseadapter class is inherited. baseadapter is an abstract class and must implement the following method in its subclass:

• Int getcount () returns the total number of records in the total data source;
• Object getitem (INT position) obtains data of a project in the selected data source based on the location of the selected project;
• Long getitemid (INT position) based on the position of the selected project;
• View getview (INT position, view convertview, viewgroup parent) obtains the project view object to be displayed.
The most troublesome method here is getview (). The getview () method is called when every list item in listview is drawn on the screen. One of the parameters in this method is convertview. convertview is null when listview displays the list items for the first time. When sliding up the screen, the list items on the screen exit the screen. The list items that are not visible under the screen will enter the screen. At this time, convertview is not a null value, the following code is crucial for providing the listview control to improve the performance.

If (convertview = NULL ){
Convertview = minflater. Inflate (R. layout. Main, null );

Holder = new viewholder ();
Holder. Text = (textview) convertview
. Findviewbyid (R. Id. textview );
Holder. Icon = (imageview) convertview. findviewbyid (R. Id. Icon );

Convertview. settag (holder );
} Else {
Holder = (viewholder) convertview. gettag ();
}
The control is instantiated only when convertview is null. A convertview object and a holder object are created. The convertview object uses minflater. inflate (R. layout. main, null) method, from a main. added and created in the XML layout file.

The convertview does not instantiate the control when it is not null. Otherwise, the control will be instantiated every time. When there are many list items, the user will slide the Screen repeatedly and the screen will feel "stuck" and it will not be smooth anymore.
The viewholder class encapsulates controls in each project. You can create an instance holder for the viewholder class when convertview is null, and then use convertview. settag (holder); Put It In convertview, and when convertview is not null, use convertview. gettag () is an instance of the viewholder class, so that the viewholder instance object will not be repeatedly created during screen flip. In this example, only nine viewholder instances are created.

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.