Android ListView Introduction and optimization Plan _android

Source: Internet
Author: User

XML design

<?xml version= "1.0"?>
-<relativelayout tools:context= ". Mainactivity "android:paddingtop=" @dimen/activity_vertical_margin "android:paddingright=" @dimen/activity_ Horizontal_margin "android:paddingleft=" @dimen/activity_horizontal_margin "android:paddingbottom=" @dimen Activity_vertical_margin "android:layout_height=" match_parent "android:layout_width=" Match_parent "xmlns:tools=" Http://schemas.android.com/tools "xmlns:android=" http://schemas.android.com/apk/res/android ">
< ListView android:id= "@+id/lv_simple" android:layout_height= "fill_parent" android:layout_width= "Fill_parent"/>
</RelativeLayout>

Main Java

Package Com.itheima.listview;
Import Java.util.HashMap;
Import Java.util.Map;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.view.TextureView;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.ListView;
Import Android.widget.TextView; public class Mainactivity extends activity {private context mcontext; @Override protected void onCreate (Bundle savedinsta Ncestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); MContext =this;//1.
Find ListView ListView lv_simple = (ListView) Findviewbyid (r.id.lv_simple);
2. Create a Adapter object Mylistadapter ListAdapter = new Mylistadapter ();
3. Set the adapter to ListView Lv_simple.setadapter (ListAdapter); Class Mylistadapter extends Baseadapter {private Map<integer, integer> Map = new Hashmap<integer, integer> (
); GetCount method: Tell ListView how many entries to display @Override public int GetCount() {return 20;}//Postion get the bean data for the entry on ListView, which does not affect the presentation of the data, can be implemented without @Override public Object getitem (int position) {RET
Urn null; //getitemid: Used to get the ID of the entry postion row, which does not affect the presentation of the data, you can first not implement @Override public long getitemid (int position) {return 0;}//getview: Tells ListView what is displayed on the entry; Returns a View object as shown on the item, which returns what view is displayed on the View,listview entry.
must implement///screen every item GetView method will be called once; Convertview: Once used View object, can be reused, before use to judge. @Override public view GetView (int position, View Convertview, ViewGroup parent) {TextView view = null; if (Convertview!= NULL) {//To determine whether the Converview is empty, and not to use view = (TextView) convertview for NULL;} 
else{view = new TextView (mcontext);//Create a TextView object} view.settext ("postion:" +position);/Set TextView content
View.settextsize (40);
Map.put (View.hashcode (), 1);
System.out.println ("Created" +map.size () + "TextView object");
return view; }
}
}

Teacher Notes

# 5 ListView Entry

ListView is a control, a control that displays entries in a vertically scrolling list, and the contents of the entries come from a listadapter. EditText Button TextView ImageView Checkbox five major layouts.

1. Layout Add ListView

2. Find ListView

3. Create a adapter adapter inherits Baseadapter, encapsulates 4 methods, where Getcount,getview must encapsulate

GetCount: Tell listview the number of entries to display

GetView: Tell listview what each entry displays.

4. Create an object for the adapter, set to ListView.

Listview.setadapter (ListAdapter adapter);

#6 ListView Optimization

The GetView method in the adapter will be passed in a Convertview,convertview is a view object that has been used, can be reused, but it needs to be judged to be empty before use, not NULL to be reused, and as the return object of the GetView method.

TextView view = null;
if (Convertview!= null) {//To determine if the Converview is null and NOT NULL to reuse
view = (TextView) Convertview;
} else{
view = new TextView (mcontext);//Create a TextView object
} return
view;

The above is a small set to introduce the Android ListView Introduction and Optimization program, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.