This article demonstrates an example of switching to display different views in the same activity. Create a viewstackactivity class to manage and cache the view. Paste the code above:
<Br/> public abstract class viewstackactivity extends activity {</P> <p> abstractmap <integer, View> views = new hashmap <integer, View> (); </P> <p>/** <br/> * @ see android. app. activity # oncreate (bundle) <br/> */<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> This. loadviews (this. getviewids (); <br/>}</P> <p> void loadviews (INT [] IDs) <br/>{< br/> layoutinflater flater = This. getlayoutinflater (); <br/> for (INT I = 0; I <IDs. length; ++ I) {<br/> View view = flater. inflate (IDs [I], null); <br/> This. onviewcreated (IDs [I], view); <br/> views. put (New INTEGER (IDs [I]), view); <br/>}< br/> public abstract int [] getviewids (); <br/> public abstract void onviewcreated (int id, view); </P> <p> void loadview (int id) <br/>{< br/> View view = This. getlayoutinflater (). inflate (ID, null); <br/> This. onviewcreated (ID, view); <br/> views. put (New INTEGER (ID), view); <br/>}</P> <p> void setactiveview (int id) <br/>{< br/> View view = views. get (New INTEGER (ID); <br/> If (view = NULL) {<br/> toast. maketext (this, "view ID:" + ID + "not found. ", toast. length_short ). show (); <br/> return; <br/>}< br/> This. setcontentview (View); <br/>}< br/>
The viewstackactivity class has two Abstract Functions: getviewids and onviewcreated, which need to be implemented by subclass. The former is used to obtain the view ID array, and the latter is used to initialize the view for the first time. Example:
<Br/> public class mainactivity extends viewstackactivity {<br/>/** called when the activity is first created. */<br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> This. setactiveview (R. layout. viewstack1); <br/>}< br/> @ override <br/> Public int [] getviewids () {<br/> return New int [] {R. layout. viewstack1, R. layout. viewstack2 }; <br/>}< br/> @ override <br/> Public void onviewcreated (int id, view) {<br/> If (ID = R. layout. viewstack1) {<br/> button btn1 = (button) view. findviewbyid (R. id. button01); <br/> btn1.setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view v) <br/>{< br/> toast. maketext (getbasecontext (), "click button02 to switch to another view... ", toast. length_short ). show (); <br/>}< br/>}); </P> <p> button btn2 = (button) view. findviewbyid (R. id. button02); <br/> btn2.setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view v) <br/>{< br/> setactiveview (R. layout. viewstack2); <br/>}< br/>}); <br/>}< br/> else if (ID = R. layout. viewstack2) {<br/> button btn1 = (button) view. findviewbyid (R. id. button01); <br/> btn1.setonclicklistener (New button. onclicklistener () {<br/> Public void onclick (view v) <br/>{< br/> setactiveview (R. layout. viewstack1); <br/>}< br/>}); </P> <p >}< br/>}
For the definition of resource ID, see download package.
Summary: This article provides a viewstackactivity class for view switching based on the requirements of a post in the Forum. The view organization method is ever-changing. It may not be a good choice to concentrate the view in an activity. In particular, the simple stacking of views with low relevance during switching will increase the complexity of future maintenance.
Attachment package: viewstackex.7z