Recently learning Android, see the ListView adaptor optimized here to meet a little understanding of the difficulties.
The current popular optimization method is to combine Convertview and viewholder to optimize view creation and lookup, such as:
Public classViewholderadapterextendsArrayadapter {Static classViewholder {TextView Text1; TextView Text2; TextView Longtext; } PublicView GetView (intposition, View Convertview, ViewGroup parent) {LOG.D (TAG,"Position=" +position); Viewholder Holder; if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item, parent,false); Holder=NewViewholder (); Holder.text1=(TextView) Convertview.findviewbyid (R.ID.TEXT1); Holder.text2=(TextView) Convertview.findviewbyid (R.ID.TEXT2); Holder.longtext=(TextView) Convertview.findviewbyid (R.id.longtext); Convertview.settag (holder); } Else{Holder=(Viewholder) Convertview.gettag (); } MYOBJ Data=GetItem (position); Holder.text1.setText (DATA.TEXT1); Holder.text2.setText (DATA.TEXT2); Holder.longtext.setText (Data.longtext); returnConvertview; }}
The acquisition of Convertview involves the recycle problem of the ListView, not the focus of this article. Here is the main view of the implementation of Viewholder. The implementation of Viewholder relies on the view provided by the Settag and Gettag two methods, in order to facilitate understanding of the two methods and the behavior of the objects maintained, here is an example:
Xml:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.settagandgettag.MainActivity" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text="" /> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/textview1"Android:layout_margintop= "120DP"Android:layout_torightof= "@+id/textview1"Android:text= "button" /></Relativelayout>
Mainactivity.java
PackageCom.example.settagandgettag;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsactionbaractivity {Button btn=NULL; TextView TV=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (R.ID.TEXTVIEW1); BTN=(Button) Findviewbyid (R.id.button1); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {MyObject obj=NewMyObject (); Obj.str= "Str before change"; Tv.settag (obj); LOG.V ("#1", ((MyObject) Tv.gettag ()). Str); Obj.str= "Str after change"; LOG.V ("#2", ((MyObject) Tv.gettag ()). Str); } }); } Public classmyobject{ PublicString str = ""; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
Note: Through the above example, it can be found that Settag maintains a mutable object (if the object has other references, it can be modified outside of Gettag), rather than assume that its behavior is to map the object used to Settag as a static, Immutable data (after all, Android also provides the Fingviewbytag method, referring to the Findviewbyid, according to the common sense, as the ID of the key value should not be able to be modified, there is a misunderstanding before understanding)
The log following the trigger button click is as follows:
An example of Settag and Gettag behavior of view in Android