Notifydatasetchanged () Method and Observer pattern for Android source code and design mode

Source: Internet
Author: User

Baseadapter after calling the Notifydatasetchanged () method, the GridView is refreshed, the following from the source point of view of this principle is analyzed.

First go to Baseadapter to view its notifydatasetchanged () method, and discover that it called the datasetobservable notifychanged() method

 Public Abstract classbaseadapter{Private FinalDatasetobservable mdatasetobservable =Newdatasetobservable (); Public voidRegisterdatasetobserver (Datasetobserver observer) {MDATASETOBSERVABLE.REGISTEROBSERVER (Observer); }     Public voidUnregisterdatasetobserver (Datasetobserver observer) {MDATASETOBSERVABLE.UNREGISTEROBSERVER (Observer); }...     Public voidnotifydatasetchanged () {mdatasetobservable.notifychanged (); }...}

Then look at the notifychanged () method of datasetobservable and discover that it called the OnChanged () method of Mobservers. Get (i), That is, the onchanged () method of Datasetobserver.

 Public class extends Observable<datasetobserver> {...      Public void notifychanged () {        synchronized(mobservers) {for             (int i = Mobservers.size ()-1; I >= 0; i--) {                mobservers.get (i). onChanged ();}}    }
...}

The Mobservers is assigned here.

 Public Abstract classObservable<t> {       protected FinalArraylist<t> mobservers =NewArraylist<t>();  Public voidRegisterobserver (T observer) {if(Observer = =NULL) {            Throw NewIllegalArgumentException ("The Observer is null.")); }        synchronized(mobservers) {if(Mobservers.contains (Observer)) {Throw NewIllegalStateException ("Observer" + Observer + "is already registered.");        } MOBSERVERS.ADD (Observer); }    }}

Next, look for the Registerobserver () method where it was called, and discover just how the Baseadapter registerdatasetobserver() method called it.

This method is called in the Setadapter method of the GridView.

...classGridView ... {... @Override Public voidSetadapter (ListAdapter adapter) {...resetlist ();                Mrecycler.clear (); Madapter=adapter;
...
if(Madapter! =NULL) { ...Mdatasetobserver=NewAdapterdatasetobserver (); Madapter.registerdatasetobserver (Mdatasetobserver); ... } Else{checkfocus (); //Nothing selectedcheckselectionchanged (); } requestlayout (); }...}

In the Adapterdatasetobserver class, the specific refresh action is done in the onchanged () method of this class.

classAdapterdatasetobserverextendsDatasetobserver2   { 3PrivateParcelable minstancestate =NULL; 4 5Adapterdatasetobserver () {6     } 7 Public voidOnChanged () {mdatachanged =true; 8 Molditemcount =Mitemcount;9 Mitemcount =Getadapter (). GetCount ();10 11if((Getadapter (). Hasstableids ()) && (minstancestate! =NULL) && (molditemcount = 0) && (Mitemcount > 0))12       {13onrestoreinstancestate (minstancestate);Minstancestate =NULL;15}Else {16remembersyncstate ();17       }18Checkfocus ();19requestlayout ();20     }21st//... Omit unnecessary code22}

It calls the Requestlayout () method, and then calls the scheduletraversals() method, which is a related method to refresh the view, and the implementation of this article is not analyzed.

1 public     void  requestlayout () {2         checkthread (); 3         true; 4         scheduletraversals (); 5     }

Here you can see how notifydatasetchanged is implemented in a step-by-step way.

You can see that this process takes full advantage of the observer pattern to excite the view refresh.

View.setadapter (adapter) incoming adapter is the observer, and the observer is the member variable mdatasetobserver of the GridView parent class Abslistview. Therefore the refreshed interface is designated for this GridView.

The observer related classes here are implemented by Android, but they are the same as in Java implementations.

Notifydatasetchanged () Method and Observer pattern for Android source code and design mode

Related Article

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.