Summarize some of the things I should be aware of when using the Android ListView:
1. To handle a click event for a child control in a ListView item item, you need two steps:
(1) Overriding the Ontouchevent method of a child control
@Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubreturn false;}
(2) Add a sentence to the root layout in the XML file of item:
2.listview optimization Strategy Viewholder, code examples are as follows:
Class Myadapter extends Baseadapter{context mcontext; LinearLayout linearlayout = null; Layoutinflater Inflater; TextView tex;final int view_type = 2;final int type_1 = 0;final int type_2 = 1;public Myadapter (context context) {Mcontext = Context;inflater = Layoutinflater.from (Mcontext);} @Overridepublic int GetCount () {return liststring.size ();} Each convert view calls this method to get the currently required view style @overridepublic int getitemviewtype (int position) {int p = position%6;if (P = = 0) Return Type_1;else if (P < 3) return Type_2;elsereturn Type_1;} @Overridepublic int Getviewtypecount () {return 2;} @Overridepublic Object getItem (int arg0) {return liststring.get (arg0);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {ViewHolder1 Holder1 = Null;viewholder2 Hol Der2 = Null;int type = getitemviewtype (position);//no convertview, need to new out each control if (Convertview = = null) {//By the current desired style, Determine the layout of new switch (type) {Case Type_1:convertview = INFLATER.INFLAte (R.layout.listitem1, parent, false); holder1 = new ViewHolder1 (); Holder1.textview = (TextView) Convertview.findviewbyid (r.id.textview1); Holder1.checkbox = (CheckBox) Convertview.findviewbyid (R.id.checkbox); Convertview.settag (holder1); Break;case Type_2:convertview = Inflater.inflate (r.layout.listitem2, parent, false); Holder2 = new ViewHolder2 (); Holder2.textview = (TextView) Convertview.findviewbyid (R.ID.TEXTVIEW2); Holder2.imageview = (ImageView) Convertview.findviewbyid (R.id.imageview); Convertview.settag (holder2); break;}} else{//has Convertview, by style, gets no layout switch (type) {case type_1:holder1 = (viewHolder1) convertview.gettag (); break;case Type_2:holder2 = (viewHolder2) convertview.gettag (); break;} Set resource switch (type) {case TYPE_1:holder1.textView.setText (integer.tostring (position)); holder1.checkBox.setChecked (true); Break;case TYPE_2:holder2.textView.setText (integer.tostring (position)); Holder2.imageView.setBackgroundResource (R.drawable.icon); break;}} return Convertview;}} Control resources for each layout static class viewhoLder1{checkbox CheckBox; TextView TextView;} Static class Viewholder2{imageview ImageView; TextView TextView;}
The listener event for the 3.Listview neutron control needs to be written in the adapter GetView method, and not in the activity as much as possible. If you need to use Viewholder in GetView, the Viewholder should be written in a different way than it was originally written. The following sections should not appear:
if (view = = null) {...} Else{holder = (Viewholder) view.gettag (); ...}
Android ListView Usage Considerations