When the intermediate container changes, the title bar changes as well.
To design a comparison basis:
Abstract classes define abstract methods in Baseview, each inherited View must be implemented to define a unique int constant for its own interface , as a comparison basis
Reduce the coupling between the containers:
The intermediate container dries the rest of the container, the coupling is high, and the intermediate container should simply "notify" the other containers.
Use observer mode to increase monitoring of intermediate container changes to solve problems if there are multiple associative containers
Viewer mode:
Observed object ======> Intermediate container
Observer ============> header Container
observed objects {List< viewer > lists; boolean ischange=false; Setchange () {Ischange=true; } notify () { for(Observer item:lists) {item.update (); }}}
Mainactivity.java
/** * Initialization principal */public void Initmiddle () { Maincontainer =(relativelayout) Findviewbyid (r.id.il_main); Middlemanager.getinstance (). Setmaincontainer (Maincontainer); Middlemanager.getinstance (). Loadview (LoginView. class ); // Add Observer middlemanager.getinstance (). Addobserver (Titlemanager.getinstance ()); }
Middlemanager.java
/** * Modify Title * /public void changetitle () { setchanged (); Notifyobservers (Currentview.getid ()); }
Titlemanager.java
/*** Modify Title*/@Override Public voidUpdate (Observable Observable, Object data) {if(data!=NULL&&Stringutils.isnumeric (data.tostring ())) { Switch(Integer.parseint (data.tostring ())) { CaseConstantValue.VIEW_INDEX:showLoginTitle (); Break; CaseConstantValue.VIEW_LOGIN:showUnloginTitle (); Break; } } }
Indexview.java
/** * Return ID */ @Overridepublicint GetID () { return constantvalue.view_index; }
Constantvalue.java
PackageCom.tsh.lottery;/*** Constant class *@authorTaoshihan **/ Public classConstantvalue { Public Final Static intView_index=1;//Home Interface ID Public Final Static intview_login=2;//Login Interface ID}
1. Intermediate container becomes the object to be observed, inheriting Observable
2. Header container becomes observer, implements Observer interface
3. Establish a relationship between the observer and the Observer, and the title is added to the array of intermediate containers
4. Once the intermediate container changes, modify the boolean value, and then notify all observers . Update ()
[Android] through the comparison of the container linkage