Self-customization of the ListView

Source: Internet
Author: User

(1) Create the project, import the picture in the Res/drawable_hdpi folder, add the ListView control in the Activity_main.xml file, add the code as follows

<listview android:id= "@+id/list_view" android:layout_width= "match_parent" android:layout_height= "Match_parent" ></ListView>

(2) Customize the layout file to the ListView cell, create a new file Fruit_item.xml file under the Res/layout directory, the code is as follows

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:orientation=" vertical ">        <imageview         Android:id= "@+id/fruit_image"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" />    <textview         android:id= "@+id/fruit_name"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content"        android:layout_gravity= "center"        android:layout_marginleft= "10dip"/> </LinearLayout>

(3) Define an entity class as the appropriate type for the ListView adapter, create a new class fruit, with the code as follows

public class Fruit {private string name;private int imageid;public Fruit (String name, int imageId) {this.name=name;this.im Ageid=imageid;} Public String GetName () {return name;} public int Getimageid () {return imageId;}}

(4) Customizing an adapter, inheriting from Arrayadapter, generic specified as fruit, new class Fruitadapter, code as follows

public class Fruitadapter extends arrayadapter<fruit> {private int resourceid;public Fruitadapter (Context context , int textviewresourceid,list<fruit> objects) {Super (context, Textviewresourceid, objects); resourceid= Textviewresourceid;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubfruit fr Uit=getitem (position);//Gets the fruit instance of the current item 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 the 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;}}

The Convertview here is used to cache previously loaded layouts for later reuse. The code above indicates that if Convertview is empty, the layout is loaded with Layoutinflater, not empty, and the convertview is reused directly so that the layout can be reloaded again without having to swipe each time.

Here we also add an inner class Viewholder,convertview to create the Viewholder object when it is empty, and store the instance of the control in Viewholder. The Settag method is then called to store the Viewholder object in the view, and the Viewholder is taken out using Gettag when not empty. It is not necessary to use the Findviewbyid () method to get the instance every time after the instance of the control is cached.

Both of these steps have been optimized to improve the efficiency of the ListView operation.

(5) Modify the code in the Mainactivity, the code is as follows

public class Mainactivity extends Activity {private list<fruit> fruitlist=new arraylist<fruit> (); overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initfruit ();//Initialize Data fruitadapter adapter=new fruitadapter (Mainactivity.this, R.layout.fruit_item, fruitlist); ListView listview= (ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); Listview.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> Parent, view view, int position, long id) {Fruit fruit=fruitlist.get (position); Toast.maketext (Mainactivity.this, Fruit.getname (), Toast.length_short). Show ();}}); private void Initfruit () {Fruit fruit1=new Fruit ("Fruit1", r.drawable.a1); Fruitlist.add (FRUIT1); Fruit fruit2=new Fruit ("Fruit2", r.drawable.a2); Fruitlist.add (FRUIT2); Fruit fruit3=new Fruit ("Fruit3", r.drawable.a3); Fruitlist.add (FRUIT3); Fruit fruit4=new Fruit ("Fruit4", r.drawable.a4); FruiTlist.add (FRUIT4); Fruit fruit5=new Fruit ("Fruit5", R.drawable.a5); Fruitlist.add (FRUIT5); Fruit fruit6=new Fruit ("Fruit6", r.drawable.a6); Fruitlist.add (FRUIT6); Fruit fruit7=new Fruit ("Fruit7", R.drawable.a7); Fruitlist.add (FRUIT7); Fruit fruit8=new Fruit ("Fruit8", r.drawable.a8); Fruitlist.add (FRUIT8); Fruit fruit9=new Fruit ("Fruit9", r.drawable.a9); Fruitlist.add (FRUIT9); Fruit fruit10=new Fruit ("fruit10", r.drawable.a10); Fruitlist.add (fruit10); Fruit fruit11=new Fruit ("fruit11", r.drawable.a11); Fruitlist.add (fruit11); Fruit fruit12=new Fruit ("fruit12", R.drawable.a12); Fruitlist.add (fruit12); Fruit fruit13=new Fruit ("fruit13", R.drawable.a13); Fruitlist.add (fruit13); Fruit fruit14=new Fruit ("fruit14", r.drawable.a14); Fruitlist.add (fruit14);}}

The results of the operation are as follows:

It is not difficult to see from the above steps that we can customize a wide range of interfaces just by modifying the files in step (2).

Self-customization 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.