Android Actionbar (ii): Actionbar Implement Tabs tags and drop-down navigation

Source: Internet
Author: User

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:

  1. Private class Mytablistener implements Actionbar.tablistener {
  2. private tabcontentfragment mfragment;
  3. Public Tablistener (tabcontentfragment fragment) {
  4. Mfragment = fragment;
  5. } @Override
  6. public void ontabselected (Tab tab, fragmenttransaction ft) {
  7. Ft.add (R.id.fragment_content, mfragment, null);
  8. }
  9. @Override
  10. public void ontabunselected (Tab tab, fragmenttransaction ft) {
  11. Ft.remove (mfragment);
  12. }
  13. @Override
  14. public void ontabreselected (Tab tab, fragmenttransaction ft) {
  15. }
  16. }


Next we create actionbar in the activity, the code is as follows;

  1. <strong>@Override
  2. protected void OnCreate (Bundle savedinstancestate) {
  3. Super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.main);
  5. Final ActionBar ActionBar = Getactionbar (); //Hint Getactionbar method must be behind Setcontentview
  6. Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
  7. Actionbar.setdisplayoptions (0, Actionbar.display_show_title);
  8. Fragment artistsfragment = new Artistsfragment ();
  9. Actionbar.addtab (Actionbar.newtab (). SetText (r.string.tab_artists). Settablistener (new Tablistener (  (artistsfragment)));
  10. Fragment albumsfragment = new Albumsfragment ();
  11. Actionbar.addtab (Actionbar.newtab (). SetText (R.string.tab_albums). Settablistener (new Tablistener (  (albumsfragment)));
  12. }</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

    1. <strong>actionbar ActionBar = Getactionbar ();
    2. Actionbar.setnavigationmode (actionbar.navigation_mode_list);
    3. Actionbar.setlistnavigationcallbacks (Mspinneradapter, Mnavigationcallback);</strong>


Above we bind a Spinneradapter control through the Setlistnavigationcallbacks method, the concrete Onnavigationlistener code example is;

  1. <strong> Monnavigationlistener = new Onnavigationlistener () {
  2. String[] strings = Getresources (). Getstringarray (R.array.action_list);
  3. @Override
  4. Public boolean onnavigationitemselected (int position, long itemId) {
  5. Listcontentfragment newfragment = new Listcontentfragment ();
  6. Fragmenttransaction ft = openfragmenttransaction ();
  7. Ft.replace (R.id.fragment_container, Newfragment, strings[position]);
  8. Ft.commit ();
  9. return true;
  10. }
  11. };</strong>



And the code for the Listcontentfragment is:

    1. Public class Listcontentfragment extends Fragment {
    2. Private String MText;
    3. @Override
    4. Public void Onattach (activity activity) {
    5. Super.onattach (activity);
    6. MText = Gettag ();
    7. }
    8. @Override
    9. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
    10. Bundle savedinstancestate) {
    11. TextView Text = new TextView (Getactivity ());
    12. Text.settext (MText);
    13. return text;
    14. }
    15. }
Copy to Google TranslateTranslation Results

the JAVA"In viewplaincopy

Android Actionbar (ii): Actionbar Implement Tabs tags and drop-down navigation

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.