Simple code example:
Package Com.edaixi.adapter;import Android.content.context;import Android.view.view;import android.view.ViewGroup; Import Android.widget.relativelayout;import Android.widget.textview;import Com.edaixi.activity.r;import Com.edaixi.dataset.myaddressdataset;public class Selectaddresslistadapter extends Basicadapter {private Myaddressdataset mtruedataset = null;private Myaddressdataset mfalsedataset = null;private Context context;public static int true_title = 1;public static int true_item = 2;public selectaddresslistadapter (Context mcontext,myaddressdataset MTru Edataset, Myaddressdataset Mfalsedataset) {super (mcontext); this.mtruedataset = Mtruedataset;this.mfalsedataset = Mfalsedataset;this.context = Mcontext;} @Overridepublic int GetCount () {if (mtruedataset.size () > 0 && mfalsedataset.size () > 0) {return mtruedatase T.size () + mfalsedataset.size () + 2;} else if (mtruedataset.size () = = 0 && mfalsedataset.size () = = 0) {return mtruedataset.size () + mfalsedataset.size () ;}else {return mtruedataset.size () + mfalsedataset.size () + 1;}} @Overridepublic Object getItem (int position) {if (position = = 0) {return null;} else if (Position < mtruedataset.size () + 1) {return Mtruedataset.getindexbean (position);} else {return Mfalsedataset.getindexbean (position- Mtruedataset.size ()-2);}} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic int Getitemviewtype (int position) {if ((mtruedataset.size () > 0) && (position = = 0)) {return TR Ue_title;} else if ((mfalsedataset.size () > 0) && (position = = Mtruedataset.size () + 1)) {return true_title;} else {return True_item;}} @Overridepublic int Getviewtypecount () {return 3;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholderitem mviewholder = null; Viewholdertitle mviewholdertitle = null;final int _position = Position;int ViewType = getitemviewtype (position); if (conve Rtview = = null) {if (ViewType = = 2) {Mviewholder = new Viewholderitem ();Convertview = view.inflate (context,r.layout.item_address_select, null); mviewholder.mname = (TextView) Convertview.findviewbyid (r.id.address_item_name); mviewholder.mphone = (TextView) Convertview.findviewbyid ( R.id.address_item_tel); Mviewholder.mdetailadd = (TextView) Convertview.findviewbyid (R.id.address_item_text); Mviewholder.maddlayout = (relativelayout) Convertview.findviewbyid (r.id.rl_address_select); ConvertView.setTag ( Mviewholder);} else {mviewholdertitle = new viewholdertitle (); Convertview = View.inflate (context,r.layout.address_select_true_tips_ title, null); mviewholdertitle.mtitlename = (TextView) Convertview.findviewbyid (r.id.tv_address_title); Convertview.settag (Mviewholdertitle);}} else {if (ViewType = = 2) {Mviewholder = (Viewholderitem) Convertview.gettag ();} else {mviewholdertitle = (viewholdertitle) Convertview.gettag ();}} if ((mtruedataset.size () > 0) && (position = = 0)) {MViewHolderTitle.mTitleName.setText ("available address");} else if (mFa Lsedataset.size () > 0) && (Position = = Mtruedataset.size () + 1)) {MViewHolderTitle.mTitleName.setText ("Not available Address");} else {if (position <= mtruedataset.size ()) {MViewHolder.mAddLayout.setBackgroundColor (Context.getresources ()). GetColor (R.color.white)); MViewHolder.mName.setTextColor (Context.getresources (). GetColor (r.color.text_1)); MViewHolder.mName.setText (Mtruedataset.getindexbean (_position-1). GetUserName ()); if (Mtruedataset.getindexbean (_ position-1). Gettel ()! = null) {MViewHolder.mPhone.setTextColor (Context.getresources (). GetColor (r.color.text_1)); MViewHolder.mPhone.setText (Mtruedataset.getindexbean (_position-1). Gettel ());} MViewHolder.mDetailAdd.setTextColor (Context.getresources (). GetColor (r.color.text_1)); Mtruedataset.getindexbean (_position-1). getaddress ()! = null) MViewHolder.mDetailAdd.setText ( Mtruedataset.getindexbean (_position-1). Getcity () + Mtruedataset.getindexbean (_position-1). GetArea () + Mtruedataset.getindexbean (_position-1). GetAddress (). Trim ()); else {MVIEWHOLDER.MADDLAYOUT.SETBACKGROundcolor (Context.getresources (). GetColor (R.color.gray)); MViewHolder.mName.setTextColor (Context.getresources () . GetColor (R.color.falseaddress)); MViewHolder.mName.setText (Mfalsedataset.getindexbean (Position- Mtruedataset.size ()-2). GetUserName ()); if (Mfalsedataset.getindexbean (Position-mtruedataset.size ()-2). GetTel ()! = NULL) {MViewHolder.mPhone.setTextColor (Context.getresources (). GetColor (r.color.falseaddress)); MViewHolder.mPhone.setText (Mfalsedataset.getindexbean (Position-mtruedataset.size ()-2). Gettel ()); MViewHolder.mDetailAdd.setTextColor (Context.getresources (). GetColor (r.color.falseaddress)); Mtruedataset.getindexbean (_position-mtruedataset.size ()-2). getaddress ()! = null) MViewHolder.mDetailAdd.setText ( Mfalsedataset.getindexbean (Position-mtruedataset.size ()-2). Getcity () + Mfalsedataset.getindexbean (Position- Mtruedataset.size ()-2). Getarea () + Mfalsedataset.getindexbean (Position-mtruedataset.size ()-2). GetAddress (). Trim ( ));}} return Convertview;} private staticClass Viewholderitem {private TextView mname;private TextView mphone;private TextView mdetailadd;private relativelayout Maddlayout;} private static class Viewholdertitle {private TextView mtitlename;}}
Listview Multiple Viewholder implementations