As we all know, the data in the ListView is adapter adapted to the screen, so to implement the contents of each item in the ListView needs to manipulate the code in the adapter:
We will refactor the following two methods when using different layouts: Getitemviewtype and Getviewtypecount.
public class Myadapter extends Baseadapter {arraylist<string> ls; Context Mcontext; LinearLayout linearlayout = null; Layoutinflater Inflater; TextView Tex; Final int view_type = 3; Final int type_1 = 0; final int type_2 = 1; final int type_3 = 2; Public Myadapter (arraylist<string> list, context context) {Mcontext = context; ls = list; } @Override public int getitemviewtype (int position) {return super.getitemviewtype (position); } @Override public int getviewtypecount () {return 3; } @Override public int getcount () {return ls.size (); } @Override public Object getItem (int position) {return ls.get (position); } @Override public long getitemid (int position) {return 0; } @Override public View getView (int position, view Convertview, ViewGroup parent) {ViewHolder1 Holder1 = nul L ViewHolder2 holder2 = null; ViewHolder3 holdEr3 = null; int type = Getitemviewtype (position); if (Convertview = = null) {Inflater = Layoutinflater.from (Mcontext); Switch (type) {Case type_1:convertview = inflater.inflate (r.layout.layout_item1, Paren T, false); Holder1 = new ViewHolder1 (); HOLDER1.TV1 = (TextView) Convertview.findviewbyid (R.ID.TV1); Convertview.settag (Holder1); Break Case Type_2:convertview = Inflater.inflate (r.layout.layout_item2, parent, false); Holder2 = new ViewHolder2 (); HOLDER2.TV2 = (TextView) Convertview.findviewbyid (R.ID.TV2); Convertview.settag (HOLDER2); Break Case Type_3:convertview = inflater.inflate (R.layout.layout_item3,parent,false); Holder3 = new ViewHolder3 (); holder3.tV3 = (TextView) Convertview.findviewbyid (R.ID.TV3); Convertview.settag (HOLDER3); }}else{switch (type) {case type_1:holder1 = (ViewHolder1) Convertvie W.gettag (); Break Case type_2:holder2 = (ViewHolder2) convertview.gettag (); Break Case Type_3:holder3 = (ViewHolder3) convertview.gettag (); Break }} switch (type) {case TYPE_1:holder1.tv1.setText ("I am the first" + (position + 1)); Break Case TYPE_2:holder2.tv2.setText ("I am the second" + (position + 1)); Break Case TYPE_3:holder3.tv3.setText ("I am three" + (position + 1)); Break } return Convertview; } public static class ViewHolder1 {TextView TV1; } public static Class viewholder2 {TextView tv2; } public static class ViewHolder3 {TextView TV3; }}
Create a different viewholder by calling the type returned to determine which item is available.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Each item in the Android ListView has a different layout