Android listview custom adapter and UI Optimization

Source: Internet
Author: User

Address of this article: http://blog.csdn.net/csheng1204/article/details/7233910 reprint please note, thank you ~~

In fact, there are quite a lot of such examples on the Internet, but I 'd like to write it myself. It should be a review method.

For more information about how to use listactivity, see:

Listactivity usage and setemptyview of listview

 

The following adapter has static class viewholder, which means that the getview method runs in the UI thread. To avoid blocking the UI, it must be optimized to make the card feel. For getview optimization, to learn more, please use the search engine properly ~~

Listviewadapter. Java

Package COM. csheng. asynctask; import Java. util. arraylist; import Java. util. list; import android. content. context; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. imageview; import android. widget. textview; public class listviewadapter extends baseadapter {public static final string tag = "listviewadapter"; PR Ivate list <string> STRs = new arraylist <string> (); private context mcontext = NULL; Public listviewadapter (context) {mcontext = context; For (INT I = 0; I <10; I ++) {STRs. add ("test string" + I) ;}@ override public int getcount () {If (! STRs. isempty () {return STRs. Size () ;}else {return 0 ;}@ override public object getitem (INT position) {If (! STRs. isempty () {return STRs. get (position) ;}else {return NULL ;}@ override public long getitemid (INT position) {return position ;}static class viewholder {imageview logoiv; textview texttv ;} /*** it is run in the UI thread, so it is necessary to reduce unnecessary new items, only to retrieve data and set data operations * the most important part of custom adapter, use static class viewholder to reduce the number of new objects */@ override public view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; If (convertview = NULL) {convertview = layoutinflater. from (mcontext ). inflate (R. layout. list_item, null); holder = new viewholder (); holder. logoiv = (imageview) convertview. findviewbyid (R. id. logoiv); holder. texttv = (textview) convertview. findviewbyid (R. id. texttv); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} final string = STRs. get (position); // retrieve the string/* custom settings of the corresponding position from the STRs data source, such as configurable images and text */holder. texttv. settext (string); Return convertview ;}}

List_item.xml

 
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <imageview Android: Id = "@ + ID/logoiv" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "@ drawable/ic_launcher"/> <textview Android: Id = "@ + ID/texttv" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_torightof = "@ + ID/logoiv"/> </relativelayout>

 

 

 

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.