I. Adding tags Tabs
Implementing a tab in ActionBar can implement Android.app.ActionBar.TabListener, overriding Ontabselected, Ontabunselected and ontabreselected methods to correlate fragment. The code is as follows:
- Private class Mytablistener implements Actionbar.tablistener {
- private tabcontentfragment mfragment;
- Public Tablistener (tabcontentfragment fragment) {
- Mfragment = fragment;
- } @Override
- public void ontabselected (Tab tab, fragmenttransaction ft) {
- Ft.add (R.id.fragment_content, mfragment, null);
- }
- @Override
- public void ontabunselected (Tab tab, fragmenttransaction ft) {
- Ft.remove (mfragment);
- }
- @Override
- public void ontabreselected (Tab tab, fragmenttransaction ft) {
- }
- }
Next we create actionbar in the activity, the code is as follows;
- <strong>@Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Final ActionBar ActionBar = Getactionbar (); //Hint Getactionbar method must be behind Setcontentview
- Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
- Actionbar.setdisplayoptions (0, Actionbar.display_show_title);
- Fragment artistsfragment = new Artistsfragment ();
- Actionbar.addtab (Actionbar.newtab (). SetText (r.string.tab_artists). Settablistener (new Tablistener ( (artistsfragment)));
- Fragment albumsfragment = new Albumsfragment ();
- Actionbar.addtab (Actionbar.newtab (). SetText (R.string.tab_albums). Settablistener (new Tablistener ( (albumsfragment)));
- }</strong>
Two. Add the drop-down navigation drop-down Navigation:
Creating a Spinneradapter provides a drop-down option, and the tab mode is different drop-down only need to modify the Setnavigationmode mode, will Actionbar.navigation_mode_ Tabs changed to Actionbar.navigation_mode_list, and the final improved code is
- <strong>actionbar ActionBar = Getactionbar ();
- Actionbar.setnavigationmode (actionbar.navigation_mode_list);
- Actionbar.setlistnavigationcallbacks (Mspinneradapter, Mnavigationcallback);</strong>
Above we bind a Spinneradapter control through the Setlistnavigationcallbacks method, the concrete Onnavigationlistener code example is;
- <strong> Monnavigationlistener = new Onnavigationlistener () {
- String[] strings = Getresources (). Getstringarray (R.array.action_list);
- @Override
- Public boolean onnavigationitemselected (int position, long itemId) {
- Listcontentfragment newfragment = new Listcontentfragment ();
- Fragmenttransaction ft = openfragmenttransaction ();
- Ft.replace (R.id.fragment_container, Newfragment, strings[position]);
- Ft.commit ();
- return true;
- }
- };</strong>
And the code for the Listcontentfragment is:
- Public class Listcontentfragment extends Fragment {
- Private String MText;
- @Override
- Public void Onattach (activity activity) {
- Super.onattach (activity);
- MText = Gettag ();
- }
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container,
- Bundle savedinstancestate) {
- TextView Text = new TextView (Getactivity ());
- Text.settext (MText);
- return text;
- }
- }
Copy to Google TranslateTranslation Results
the JAVA"In viewplaincopy
Android Actionbar (ii): Actionbar Implement Tabs tags and drop-down navigation