Package Com.example.zimusortlistview; Import java.util.ArrayList; Import java.util.List; Import Android.os.Bundle; Import android.app.Activity; Import Android.view.LayoutInflater; Import Android.view.Menu; Import Android.view.View; Import Android.view.ViewGroup; Import Android.widget.AdapterView; Import Android.widget.AdapterView.OnItemClickListener; Import Android.widget.ArrayAdapter; Import Android.widget.BaseAdapter; Import Android.widget.LinearLayout; Import Android.widget.ListView; Import Android.widget.TextView; Import Android.widget.Toast; /** * @author Peng Xiangming * Load multiple layouts in the ListView * Take the city list as an example to ensure that there are no identical elements in Cityletter and CityName, and that there are no identical elements in letter */ public class Mainactivity extends Activity implements onitemclicklistener{ String[] letter={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X "," Y "," Z "}; String[] cityletter={"C", "B", "H", "G", "W", "C", "s", "s", "C", "W", "H", "Z", "C", "D", "N", "L", "Q", "s", "s" , "L", "N", "H", "X", "H", "S", "Z", "G", "N", "J", "T", "S", "X", "A"}; String[] cityname={"Changsha", "Beijing", "Hangzhou", "Guangzhou", "Wuhan", "Chongqing", "Shanghai", "Shenzhen", "Changchun", "Urumqi", "Harbin", "Zhengzhou", "Chengdu", "Dalian", "Nanchang", "Lanzhou", "Qiqihar", " Shantou "," Suzhou " , "Lhasa", "Nanjing", "Hohhot", "Xiamen", "Hefei", "Shenyang", "Zhangjiajie", "Guizhou", "Ningxia", "Jinan", "Tianjin", "Shijiazhuang", "Xi ' an", "Macau"}; List<string> lettertocity=new arraylist<string> (); int count; ListView LV; ListView Lv1; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); String str= ""; for (int i=0;i<letter.length;i++) { Str=letter[i]; Boolean isaddletter=false; for (int j=0;j<cityletter.length;j++) { if (Str.equals (Cityletter[j])) { if (!isaddletter) { Lettertocity.add (str); Isaddletter=true; } Lettertocity.add (Cityname[j]); } } } lv= (ListView) Findviewbyid (R.ID.LISTVIEW1); Lv.setadapter (New Myadapter ()); Lv.setonitemclicklistener (this); lv1= (ListView) Findviewbyid (R.ID.LISTVIEW2); Lv1.setadapter (New MyAdapter1 ()); Lv1.setonitemclicklistener (this); } Class MyAdapter1 extends baseadapter{ @Override public int GetCount () { TODO auto-generated Method Stub return letter.length; } @Override Public Object getItem (int position) { TODO auto-generated Method Stub return letter[position]; } @Override public long getitemid (int position) { TODO auto-generated Method Stub return position; } @Override Public View GetView (int position, View Convertview, ViewGroup parent) { View View=layoutinflater.from (mainactivity.this). Inflate (r.layout.letter_list, NULL); TextView tv= (TextView) View.findviewbyid (R.id.letterlisttextview); Tv.settext (Letter[position]); return view; }
}
Class Myadapter extends baseadapter{ final static int type_1=1; final static int type_2=2;
@Override public int GetCount () { TODO auto-generated Method Stub return Lettertocity.size (); } @Override Public Object getItem (int position) { TODO auto-generated Method Stub return Lettertocity.get (position); } @Override public long getitemid (int position) { TODO auto-generated Method Stub return position; }
@Override public int Getitemviewtype (int position) { for (int i=0;i<letter.length;i++) { if (Lettertocity.get (position). Equals (Letter[i])) { return type_1; } } return type_2; }
@Override public int Getviewtypecount () { return 3; } @Override Public View GetView (int position, View Convertview, ViewGroup parent) { /** * Even though Convertview caches some layouts, when refactoring, you don't know how to get Convertview back to the layout you need, you need to * To let adapter know what layouts I currently have, I'll refactor the layout selection rules when I refactor item so that Convertview can return to the layout you need. * You need to rewrite the following two functions * @Override * public int getitemviewtype (int position) {} This function gets the type of view created in GetView * @Override * public int Getviewtypecount () {} returns the number of view types created in GetView * As for the detailed use of these two methods, you can see the API */ ViewHolder1 Vh1=null; ViewHolder2 Vh2=null; int Type=getitemviewtype (position); if (convertview==null) { Switch (type) { Case Type_1: Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.letter, NULL); Vh1=new ViewHolder1 (); vh1.tv= (TextView) Convertview.findviewbyid (R.id.lettertextview); Convertview.settag (VH1); Break Case type_2: Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.city, NULL); Vh2=new ViewHolder2 (); vh2.tv= (TextView) Convertview.findviewbyid (R.id.citytextview); Convertview.settag (VH2); Break Default Break } }else{ Switch (type) { Case Type_1: vh1= (ViewHolder1) Convertview.gettag (); Break Case type_2: Vh2= (ViewHolder2) Convertview.gettag (); Break Default Break } } Switch (type) { Case Type_1: Vh1.tv.setText (Lettertocity.get (position)); Break Case type_2: Vh2.tv.setText (Lettertocity.get (position)); Break Default Break } return convertview; This method is not advisable and the view refreshes when refreshed Boolean isletter=false; for (int i=0;i<letter.length;i++) { if (Lettertocity.get (position). Equals (Letter[i])) { Isletter=true; Break // } // } Viewholder Vh=null; if (convertview==null) { Vh=new Viewholder (); if (isletter) { Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.letter, NULL); vh.tv= (TextView) Convertview.findviewbyid (R.id.lettertextview); }else{ Convertview=layoutinflater.from (Mainactivity.this). Inflate (r.layout.city, NULL); vh.tv= (TextView) Convertview.findviewbyid (R.id.citytextview); // } Convertview.settag (VH); }else{ VH = (Viewholder) convertview.gettag (); // } Vh.tv.setText (Lettertocity.get (position)); return convertview; List of cities, modifying layouts Boolean isletter=false; for (int i=0;i<letter.length;i++) { if (Lettertocity.get (position). Equals (Letter[i])) { Isletter=true; Break // } // } View view; Viewholder VH; if (convertview==null) { View=layoutinflater.from (Mainactivity.this). Inflate (r.layout.letter_city, NULL); Vh=new Viewholder (); vh.tv= (TextView) View.findviewbyid (R.id.lettertextview); Vh.ll= (LinearLayout) View.findviewbyid (R.ID.LL); Convertview=view; Convertview.settag (VH); }else{ view= (View) Convertview; Vh= (Viewholder) Convertview.gettag (); // } Vh.tv.setText (Lettertocity.get (position)); if (isletter) { Vh.ll.setBackgroundColor (Color.ltgray); Vh.tv.setPadding (5, 5, Vh.tv.getPaddingRight (), 5); }else{ Vh.ll.setBackgroundColor (Color.White); Vh.tv.setPadding (Ten, Vh.tv.getPaddingRight (), 10); // } return view; } } @Override public void Onitemclick (adapterview<?> parent, view view, int position, Long id) { Switch (Parent.getid ()) { Case R.ID.LISTVIEW1: Boolean isletter=false; for (int i=0;i<letter.length;i++) { if (Letter[i].equals (lettertocity.get (position))) { Isletter=true; Break } } if (!isletter) { Toast.maketext (this, lettertocity.get (position), Toast.length_short). Show (); } Break Case R.ID.LISTVIEW2: for (int i=0;i<lettertocity.size (); i++) { if (Letter[position].equals (Lettertocity.get (i))) { Lv.setselection (i); Break } } Break Default Break } } } Class viewholder{ TextView TV; LinearLayout ll; } Class viewholder1{ TextView TV; } Class viewholder2{ TextView TV; } |
|