详解android actionbar之三:实现actionbar tabs标签的切换
java代码
- public&class&actionbartabs&extends&activity&{&&
- &&
- @override&&
- protected&void&oncreate(bundle&savedinstancestate)&{&&
- super.oncreate(savedinstancestate);&&
- setcontentview(r.layout.action_bar_tabs);&&
- }&&
- &&
- public&void&onaddtab(view&v)&{&&
- final&actionbar&bar&=&getactionbar();&&
- final&int&tabcount&=&bar.gettabcount();&&
- final&string&text&=&"tab&"&+&tabcount;&&
- &&
- bar.addtab(bar.newtab().settext(text)&&
- .settablistener(new&tablistener(new&tabcontentfragment(text))));&&
- }&&
- &&
- public&void&onremovetab(view&v)&{&&
- final&actionbar&bar&=&getactionbar();&&
- bar.removetabat(bar.gettabcount()&-&1);&&
- }&&
- &&
- public&void&ontoggletabs(view&v)&{&&
- final&actionbar&bar&=&getactionbar();&&
- &&
- if&(bar.getnavigationmode()&==&actionbar.navigation_mode_tabs)&{&&
- bar.setnavigationmode(actionbar.navigation_mode_standard);&&
- &&
- bar.setdisplayoptions(actionbar.display_show_title,&actionbar.display_show_title);&&
- }&else&{&&
- bar.setnavigationmode(actionbar.navigation_mode_tabs);&&
- bar.setdisplayoptions(0,&actionbar.display_show_title);&&
- }&&
- }&&
- &&
- public&void&onremovealltabs(view&v)&{&&
- getactionbar().removealltabs();&&
- }&&
- &&
- private&class&tablistener&implements&actionbar.tablistener&{&&
- private&tabcontentfragment&mfragment;&&
- public&tablistener(tabcontentfragment&fragment)&{&&
- &&
- mfragment&=&fragment;&&
- }&&
- &&
- public&void&ontabselected(tab&tab,&fragmenttransaction&ft)&{&&
- ft.add(r.id.fragment_content,&mfragment,&mfragment.gettext());&&
- }&&
- &&
- &&&
- public&void&ontabunselected(tab&tab,&fragmenttransaction&ft)&{&&
- ft.remove(mfragment);&&
- }&&
- &&
- public&void&ontabreselected(tab&tab,&fragmenttransaction&ft)&{&&
- toast.maketext(actionbartabs.this,&"reselected!",&toast.length_short).show();&&
- }&&
- &&
- }&&
- &&
- private&class&tabcontentfragment&extends&fragment&{&&
- private&string&mtext;&&
- public&tabcontentfragment(string&text)&{&&
- mtext&=&text;&&
- }&&
- &&
- public&string&gettext()&{&&
- return&mtext;&&
- }&&
- &&
- @override&&
- public&view&oncreateview(layoutinflater&inflater,&viewgroup&container,&&
- bundle&savedinstancestate)&{&&
- view&fragview&=&inflater.inflate(r.layout.action_bar_tab_content,&container,&false);&&
- textview&text&=&(textview)&fragview.findviewbyid(r.id.text);&&
- text.settext(mtext);&&
- return&fragview;&&
- }&&
- }&&
- }&&