Use of the ListView in Android and boost the efficiency of the ListView

Source: Internet
Author: User

The ListView is a very common control that wirelessly can use to present a series of data, below to describe the usage of the ListView and how to improve the efficiency of the ListView

The use of the ListView follows the MVC pattern, M is the entity, V is the view (that is, the ListView), and C is the controller (that is, the adapter adapter)

Using a ListView generally follows these steps

I. Define the layout of an entry that is displayed by a ListView

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=" wrap_content ">    < ImageView        android:id= "@+id/iv_icon"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:layout_centervertical=" true "        android:layout_marginleft=" 10DP "        android:src=" @ Drawable/ic_launcher "/>    <textview        android:id=" @+id/tv_text "android:layout_width=" Wrap_        Content "        android:layout_height=" wrap_content "        android:layout_centerinparent=" true "        Android: Textsize= "20SP"        android:layout_centervertical= "true"        android:layout_marginright= "10DP"        android: text= "Test Application"/></relativelayout>

  

Ii. Defining a data Entity (JavaBean)

 PackageCom.jsako.listviewdemo.domain;Importandroid.graphics.drawable.Drawable; Public classAppInfo {PrivateString AppName; Privatedrawable AppIcon; @Override PublicString toString () {return"AppInfo [appname=" + AppName + ", appicon=" + AppIcon + "]"; }     PublicString Getappname () {returnAppName; }     Public voidsetappname (String appName) { This. AppName =AppName; }     Publicdrawable Getappicon () {returnAppIcon; }     Public voidSetappicon (drawable appIcon) { This. AppIcon =AppIcon; }        }

Third, the initialization of data (ready to display the data, can read the database access, can be downloaded through the network)

Private list<appinfo> appinfos=new arraylist<appinfo> ();
Privatevoid Initappinfos () { List<ApplicationInfo> infos= Pm.getinstalledapplications (0); AppInfo AppInfo; for (ApplicationInfo info:infos) { AppInfo=new appInfo (); Appinfo.setappicon (Info.loadicon (PM)); Appinfo.setappname (Info.loadlabel (PM). toString ()); Appinfos.add (AppInfo); } }

Iv. defining an adapter inner class

Private classMyadapterextendsbaseadapter{@Override Public intGetCount () {returnappinfos.size (); } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent)            {View view;            Viewholder Holder; if(convertview!=NULL) {View=Convertview; Holder=(Viewholder) View.gettag (); }Else{View=view.inflate (Getapplicationcontext (), R.layout.list_item,NULL); Holder=NewViewholder (); Holder.iv_icon=(ImageView) View.findviewbyid (R.id.iv_icon); Holder.tv_text=(TextView) View.findviewbyid (R.id.tv_text);            View.settag (holder);            } holder.iv_icon.setImageDrawable (Appinfos.get (position). Getappicon ());            Holder.tv_text.setText (Appinfos.get (position). Getappname ()); returnview; } @Override PublicObject GetItem (intposition) {            return NULL; } @Override Public LongGetitemid (intposition) {            return0; }                classviewholder{ImageView Iv_icon;        TextView Tv_text; }    }

Five. Locate the object for the ListView, and then set the adapter

lv=(ListView) Findviewbyid (r.id.lv_show);        Lv.setadapter (new myadapter ());

This is the basic use of the ListView, let's talk about how to improve the efficiency of the ListView

1, in the Myadapter GetView method, the ListView does not show an entry will call the method once, there is a parameter is Convertview, which is used to cache the previously loaded layout for later reuse

So we can judge Convertview, determine if it is null, reuse it if it is not NULL, or load a layout view object with the View.inflate () method if null. This reduces the overhead of memory, reusing previous caches, and improving efficiency.

2. A new internal class Viewholder is added to cache the instances of the controls in each small entry. Then, when Convertview is null, the instance of Viewholder is saved by View.settag () and the instance of Viewholder is removed by View.gettag () when convert is not empty.

Then set the corresponding value. This prevents the ListView from calling View.findviewbyid () every time the GetView is called, which can be time consuming and slow to performance. This method of Viewholder internal classes can improve efficiency.

Use of the ListView in Android and boost the efficiency of the ListView

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.