Android ListView Performance Optimization

Source: Internet
Author: User

Package com.example.ex_000_example;


Import java.util.ArrayList;

Import Android.os.Bundle;

Import android.app.Activity;

Import Android.util.Log;

Import Android.view.LayoutInflater;

Import Android.view.Menu;

Import Android.view.View;

Import Android.view.ViewGroup;

Import Android.view.View.OnClickListener;

Import Android.widget.BaseAdapter;

Import Android.widget.ImageView;

Import Android.widget.ListView;

Import Android.widget.TextView;


public class Mainactivity extends Activity implements Onclicklistener {


Ready-to-picture resources

Private int[] imageList = new int[] {r.drawable.qq0, r.drawable.qq1,

R.DRAWABLE.QQ2, R.drawable.qq3, R.drawable.qq4, R.drawable.qq5,

R.drawable.qq6, R.drawable.qq7, R.drawable.qq8, R.drawable.qq9,

R.drawable.qq10, R.drawable.qq11, r.drawable.qq12, r.drawable.qq13,

r.drawable.qq14};


Private arraylist<mydate> datelist = new arraylist<mydate> ();

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

Preparing data

for (int i = 0; i < imagelist.length; i++) {

MyDate mydate = new MyDate ("Data Comes with" +i, Imagelist[i]);

Datelist.add (mydate);

}

ListView ListView = (ListView) Findviewbyid (R.id.listview1);

Myadapter myadapter = new Myadapter ();

Setting up the adapter

Listview.setadapter (Myadapter);

}

Reduce the number of control lookups

Class Viewholder {

TextView TextView;

ImageView ImageView;

}


Class Myadapter extends Baseadapter {

Set the style and display of each line

@Override

Public View GetView (int position, View Convertview, ViewGroup parent) {

View inflate = null;

Viewholder holder = null;

if (Convertview = = null) {

Holder = new Viewholder ();

Get the Layout converter

Layoutinflater inflater = Getlayoutinflater ();

Converting a layout XML file into a layout object

Inflate = inflater.inflate (R.layout.list_item, NULL);

Find controls, be sure to find controls on the layout object

Holder.textview = (TextView) inflate

. Findviewbyid (R.id.textview1);

Holder.imageview = (ImageView) inflate

. Findviewbyid (R.id.imageview1);

Saves the object of the control to the tag of the row layout object

Inflate.settag (holder);

} else {

Let that already completely disappear layout object, to replace the upcoming

inflate = Convertview;

To remove a previously saved control from a row layout object

Holder = (viewholder) inflate.gettag ();

}

Extracting data from the container according to position

MyDate mydate = datelist.get (position);

Displaying data on a control

Holder.textView.setText (Mydate.title);

Holder.imageView.setImageResource (Mydate.image);

return inflate;

}

Determines the number of rows displayed by the ListView

@Override

public int GetCount () {

TODO auto-generated Method Stub

return Datelist.size ();

}


@Override

Public Object getItem (int position) {

TODO auto-generated Method Stub

return null;

}


@Override

public long getitemid (int position) {

TODO auto-generated Method Stub

return 0;

}


}


@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Inflate the menu; This adds items to the action bar if it is present.

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}




Class mydate{

String title;

int image;

Public MyDate (String title, int image) {

Super ();

This.title = title;

This.image = image;

}

}


}


This article from "Broken Childe" blog, please be sure to keep this source http://pogongzi.blog.51cto.com/8982426/1621265

Android ListView Performance Optimization

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.