[Android] through the comparison of the container linkage

Source: Internet
Author: User

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

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.