Android from source code see the reuse mechanism of the ListView

Source: Internet
Author: User

Whether it is Android or iOS, the list view should be the most complex control, Android in the ListView can be seen from the name of a one-dimensional array, and iOS TableView is a two-dimensional array, but actually need to pay attention to the place is similar, is the reuse mechanism, This is the best way to consider whether you can master the listview.

The common listview initialization and the code to set the adapter are as follows:

ListView ListView; Myadapter ListAdapter; Arraylist<string> Liststring;listview = (listView) This.findviewbyid (r.id.listview); liststring = new ArrayList <String> (); for (int i = 0; i < i++) {Liststring.add (integer.tostring (i));} ListAdapter = new Myadapter (this); Listview.setadapter (ListAdapter);} Class Myadapter extends Baseadapter{context mcontext; LinearLayout linearlayout = null; Layoutinflater Inflater;public Myadapter (Context context) {//TODO auto-generated constructor stubmcontext = context; Inflater = Layoutinflater.from (Mcontext);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn liststring.size ();} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn Liststring.get (arg0);} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} Public final class Viewholder{public ImageView img;public TextView title;public TextView info;public Button viewbtn;} public class Myadapter extends BaSeadapter{private layoutinflater minflater;public Myadapter (context context) {This.minflater = Layoutinflater.from ( context);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn mdata.size ();} @Overridepublic Object getItem (int arg0) {//TODO auto-generated method Stubreturn null;} @Overridepublic long getitemid (int arg0) {//TODO auto-generated method Stubreturn 0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;if (Convertview =  = null) {holder=new viewholder (); Convertview = minflater.inflate (r.layout.vlist2, null); holder.img = (ImageView) Convertview.findviewbyid (R.id.img); Holder.title = (TextView) Convertview.findviewbyid (r.id.title); holder.info = (TextView) Convertview.findviewbyid ( R.id.info) holder.viewbtn = (Button) Convertview.findviewbyid (R.ID.VIEW_BTN); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.img.setBackgroundResource ((Integer) mdata.get (position). Get ("img")); HoldeR.title.settext (String) mdata.get (position). Get ("title")), Holder.info.setText (string) mdata.get (position). Get ( "Info")); Holder.viewBtn.setOnClickListener (new View.onclicklistener () {@Overridepublic void OnClick (View v) { Showinfo ();}}); return Convertview;}}

where Setadapter is mainly used to set up data, we do not look at the ListView (source code here) of the setadapter source code

@Override public void Setadapter (ListAdapter adapter) {if (null! = Madapter) {Madapter.unregisterda        Tasetobserver (Mdatasetobserver);        } resetlist ();        Mrecycler.clear (); if (Mheaderviewinfos.size () > 0| | mfooterviewinfos.size () > 0) {madapter = new Headerviewlistadapter (mHe        Aderviewinfos, Mfooterviewinfos, adapter);        } else {madapter = adapter;        } moldselectedposition = Invalid_position;        Moldselectedrowid = invalid_row_id;            if (madapter! = null) {mareallitemsselectable = madapter.areallitemsenabled ();            Molditemcount = Mitemcount;            Mitemcount = Madapter.getcount ();            Checkfocus ();            Mdatasetobserver = new Adapterdatasetobserver ();            Madapter.registerdatasetobserver (Mdatasetobserver);            Mrecycler.setviewtypecount (Madapter.getviewtypecount ());            int position;              if (Mstackfrombottom) {  Position = Lookforselectableposition (mItemCount-1, false);            } else {position = lookforselectableposition (0, true);            } setselectedpositionint (position);            Setnextselectedpositionint (position);            if (Mitemcount = = 0) {//Nothing selected checkselectionchanged ();            }} else {mareallitemsselectable = true;            Checkfocus ();        Nothing selected checkselectionchanged ();        } if (mcheckstates! = null) {mcheckstates.clear ();    } requestlayout (); }

From the above code can be seen to be divided into two steps, the first step is the current adapter is not empty, first empty the local adapter data, and then set the new data to the ListView. There's an important class inside.
Adapterdatasetobserver

is used to store data, let's look at the code inside, there's a member variable

Mdatasetobserver

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > is this object declaration in the ListView parent class Abslistview (<a target=_blank href= "http://grepcode.com/file_/repository.grepcode.com/ Java/ext/com.google.android/android/2.2_r1.1/android/widget/abslistview.java/?v=source "> Source code in this </a>) </span>

So what is this adapterdatasetobserver, we still go to Abslistview's father Adapterview (source in this) to explore it.

Class Adapterdatasetobserver extends Datasetobserver {private parcelable minstancestate = null;            @Override public void onChanged () {mdatachanged = true;            Molditemcount = Mitemcount;            Mitemcount = Getadapter (). GetCount ();            Detect the case where a cursor is previously invalidated have//been repopulated with new data. if (AdapterView.this.getAdapter (). Hasstableids () && minstancestate! = null && mOl                Ditemcount = = 0 && mitemcount > 0) {AdapterView.this.onRestoreInstanceState (minstancestate);            Minstancestate = null;            } else {remembersyncstate ();            } checkfocus ();        Requestlayout ();            } @Override public void oninvalidated () {mdatachanged = true; if (AdapterView.this.getAdapter (). Hasstableids ()) {//Remember the curRent state for the case where we hosting activity is being//stopped and later restarted m            Instancestate = AdapterView.this.onSaveInstanceState ();            }//Data is invalid so we should reset our state molditemcount = Mitemcount;            Mitemcount = 0;            Mselectedposition = invalid_position;            Mselectedrowid = invalid_row_id;            Mnextselectedposition = invalid_position;            Mnextselectedrowid = invalid_row_id;            Mneedsync = false;            Checkselectionchanged ();            Checkfocus ();        Requestlayout ();        } public void Clearsavedstate () {minstancestate = null; }    }

From the above code can be seen, this class is actually inherited from Datasetobserver (source code here). Using aObserver mode for data processing in the ListView.

This is an abstract class, not much to say.

Not to be continued ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.