android - Switch Fragments inside a Tab -


how switch fragments inside tab?

my application contains 3 fragments, afragment, bfragment, , cfragment. these fragments, in turn, correspond own layout files: afragment contains button, , bfragment , cfragment have textview. there fourth layout file named activity_main.

now, have 4 classes, mainactivity, afragment, bfragment , cfragment. classes afragment, bfragment, cfragment typical contain oncreateview on each of them.

mainactivity contains this:

public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     appcontext = getapplicationcontext();      //actionbar      actionbar actionbar = getactionbar();      actionbar.setnavigationmode(actionbar.navigation_mode_tabs);       actionbar.tab playertab = actionbar.newtab().settext("fragment a");      actionbar.tab stationstab = actionbar.newtab().settext("fragment b");       fragment playerfragment = new afragment();      fragment stationsfragment = new bfragment();       playertab.settablistener(new mytabslistener(playerfragment));      stationstab.settablistener(new mytabslistener(stationsfragment));       actionbar.addtab(playertab);      actionbar.addtab(stationstab); 

and tablistener:

class mytabslistener implements actionbar.tablistener { public fragment fragment;  public mytabslistener(fragment fragment) {     this.fragment = fragment; }  @override public void ontabreselected(tab tab, fragmenttransaction ft) {     toast.maketext(mainactivity.appcontext, "reselected!", toast.length_long).show(); }  @override public void ontabselected(tab tab, fragmenttransaction ft) {     ft.replace(r.id.fragment_container, fragment); }  @override public void ontabunselected(tab tab, fragmenttransaction ft) {     ft.remove(fragment); } 

}

as can see, activity_main.xml contains linearlayout id 'fragment_container' tabs afragment , bfragment. want in such way clicking button in afragment switches entire afragment cfragment. how can achieve this? thank you.

(edit) did following app crashed on start. did wrong?

i added following on mainactivity.java, below actionbar sequence:

public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); appcontext = getapplicationcontext();  //actionbar .... .... .... button mbutton = (button) findviewbyid(r.id.button1);     mbutton.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {             fragmentmanager fragmentmanager = getfragmentmanager();             fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();             cfragment fragment = new cfragment();             fragmenttransaction.add(r.id.fragment_container, fragment);             fragmenttransaction.commit();         }     }); 

  • get fragment transaction using (support) fragment manager of activity.
  • replace afragment cfragment did in ontabselected
  • commit transaction.

also, please note :

  • don't keep references on fragment, prevent garbage collection occuring. should create fragments once, not every time. in tabselected, check if fragment known fragment manager (using findfragmentbytag). if not, create it. if is, re-replace it.
  • share listener, don't need 1 tab.

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -