Android Note: ListView

Source: Internet
Author: User

ListView Property

The Android:divider property allows you to specify the color of the ListView divider, #0000 to set the divider to a transparent color.

Problems with the efficiency of the ListView

Adapter three parameters int position, View Convertview, ViewGroup Parent

1, use the view directly each time to open each load, the lowest efficiency.

Layoutinflater inflater = Layoutinflater.from (Mcontext);

View view1 = inflater.inflate (R.layout.fruit_item, NULL);

2, using Convertview (cache)

if (Convertview = = null) {
View = Layoutinflater.from (GetContext ()). Inflate (resourceId, NULL);
} else {
view = Convertview;
}

3, adding the control's load to the cache

Public View GetView (int position, view Convertview, ViewGroup parent) {
Fruit Fruit = GetItem (position );
View view;
Viewholder Viewholder;
if (Convertview = = null) {
view = Layoutinflater.from (GetContext ()). Inflate (resourceId, null);
Viewholder = new Viewholder ();
Viewholder.fruitimage = (ImageView) View.findviewbyid (r.id.fruit_image);
Viewholder.fruitname = (TextView) View.findviewbyid (r.id.fruit_name);          
View.settag (Viewholder);//Store Viewholder in view
} else {
view = Convertview;
Viewholder = (Viewholder) view.gettag ();        Retrieve Viewholder
}
ViewHolder.fruitImage.setImageResource (Fruit.getimageid ());
ViewHolder.fruitName.setText (Fruit.getname ());
return view;        
}
Class Viewholder {
ImageView fruitimage;
TextView Fruitname;
}

ListView Click event

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

Listview.setadapter (adapter);

Listview.setonitemclicklistener (New Onitemclicklistener () {

public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {

Fruit Fruit = fruitlist.get (position);

Toast.maketext (Mainactivity.this, Fruit.getname (), 0). Show ();

}

});

Android Note: 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.