1. Use Cases
InBaseadapter, we often reuse convertview in the getview () method to improve performance.ConvertviewInItemFor a single layout of the same type, it can be recycled and reused, but multipleItemDifferent layout types,ConvertviewThere will be problems with the collection and reuse. For example, some behaviors are plain text, while some rows are text-and-text mixing. Here, the pure text behavior is a type of layout, and the text-and-text mixing behavior is a type of second layout. A single type of listview is very simple. The following describes the situation that listview contains multiple types of view la S.
2. listview contains layout of different items
We need to do the following:
1) override getviewtypecount ()-the number of different la s returned by this method
2) override getitemviewtype (INT)-return the corresponding item according to position
3) Create the correct convertview in getview Based on The View Item type.
3. Case studies
Import Java. util. arraylist; Import Android. App. activity; Import Android. content. context; Import Android. OS. Bundle; Import Android. util. log; Import Android. View. layoutinflater; Import Android. View. view; Import Android. View. viewgroup; Import Android. widget. baseadapter; Import Android. widget. checkbox; Import Android. widget. imageview; Import Android. widget. linearlayout; Import Android. widget. listview; Import Android. widget. textview; Public Class ListviewtestExtends Activity { /** Called when the activity is first created. */ Listview; myadapter listadapter; arraylist <String> Liststring; @ override Public Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); listview = (Listview)This . Findviewbyid (R. Id. listview); liststring = New Arraylist <string> (); For ( Int I = 0; I <100; I ++ ) {Liststring. Add (integer. tostring (I);} listadapter = New Myadapter ( This ); Listview. setadapter (listadapter );} Class Myadapter Extends Baseadapter {context mcontext; 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 (context ){ // Todo auto-generated constructor stub Mcontext = Context; Inflater = Layoutinflater. From (mcontext);} @ override Public Int Getcount (){ // Todo auto-generated method stub Return Liststring. Size ();} // This method is called for each convert view to obtain the currently required view style. @ Override Public Int Getitemviewtype ( Int Position ){ // Todo auto-generated method stub Int P = position % 6 ; If (P = 0) Return Type_1; Else If (P <3 ) Return Type_2; Else If (P <6 ) Return Type_3; Else Return Type_1;} @ override Public Int Getviewtypecount (){ // Todo auto-generated method stub Return 3 ;} @ Override Public Object getitem ( Int Arg0 ){ // Todo auto-generated method stub Return Liststring. Get (arg0) ;}@ override Public Long Getitemid ( Int Position ){ // Todo auto-generated method stub Return Position ;}@ override Public View getview ( Int Position, view convertview, viewgroup parent ){ // Todo auto-generated method stub Viewholder1 holder1 = Null ; Viewholder2 holder2 = Null ; Viewholder3 holder3 = Null ; Int Type = Getitemviewtype (position ); // No convertview, new controls are required If (Convertview = Null ) {Log. E ( "Convertview =", "null" ); // Determine the new layout based on the current style. 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); log. E ( "Convertview =", "null type_1" ); 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); log. E ( "Convertview =", "null type_2" ); Convertview. settag (holder2 ); Break ; Case Type_3: convertview = Inflater. Inflate (R. layout. listitem3, parent, False ); Holder3 = New Viewholder3 (); holder3.textview =(Textview) convertview. findviewbyid (R. Id. textview3); holder3.imageview = (Imageview) convertview. findviewbyid (R. Id. imageview); log. E ( "Convertview =", "null type_3" ); Convertview. settag (holder3 ); Break ;}} Else { // Convertview and style-based layout for unused la s Switch (Type ){ Case Type_1: holder1 = (Viewholder1) convertview. gettag (); log. E ( "Convertview !!!!!! = "," Null type_1" ); Break ; Case Type_2: holder2 = (Viewholder2) convertview. gettag (); log. E ( "Convertview !!!!!! = "," Null type_2" ); Break ; Case Type_3: holder3 = (Viewholder3) convertview. gettag (); log. E ( "Convertview !!!!!! = "," Null type_3" ); Break ;}} // Set Resources 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 )); Break ; Case Type_3: holder3.textview. settext (integer. tostring (position); holder3.imageview. setbackgroundresource (R. drawable. Icon ); Break ;} Return Convertview ;}} // Control resources for various la s Class Viewholder1 {checkbox; textview ;}
Class Viewholder2 {textview ;}
Class Viewholder3 {imageview; textview ;}}