Android ActionBar usage (2)

Source: Internet
Author: User

3. Add Tag Tabs

The implementation tab in ActionBar can implement android. app. ActionBar. TabListener, and rewrite onTabSelected, onTabUnselected, and onTabReselected methods to associate Fragment. The Code is as follows:

Java code:

  1. Private class MyTabListener implements ActionBar. TabListener {
  2. Private TabContentFragment mFragment;
  3. Public TabListener (TabContentFragment fragment ){
  4. MFragment = fragment;
  5. }
  6. @ Override
  7. Public void onTabSelected (Tab, FragmentTransaction ft ){
  8. Ft. add (R. id. fragment_content, mFragment, null );
  9. }
  10. @ Override
  11. Public void onTabUnselected (Tab, FragmentTransaction ft ){
  12. Ft. remove (mFragment );
  13. }
  14. @ Override
  15. Public void onTabReselected (Tab, FragmentTransaction ft ){
  16. }
  17. }

Copy code

Next we will create an ActionBar in the Activity. The Code is as follows:

Java code:

  1. @ Override
  2. Protected void onCreate (Bundle savedInstanceState ){
  3. Super. onCreate (savedInstanceState );
  4. SetContentView (R. layout. main );
  5. Final ActionBar actionBar = getActionBar (); // The system prompts that the 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. }

Copy code

4. Add drop-down navigation

Create a SpinnerAdapter and provide the Drop-down option. Unlike the Tab mode, you only need to modify the setNavigationMode mode and change ActionBar. NAVIGATION_MODE_TABS to ActionBar. NAVIGATION_MODE_LIST. The improved code is

Java code:

  1. Actionbar = getactionbar ();
  2. Actionbar. setnavigationmode (actionbar. navigation_mode_list );
  3. Actionbar. setlistnavigationcallbacks (mspinneradapter, mnavigationcallback );

Copy code

The above code binds a SpinnerAdapter control through the setListNavigationCallbacks method. The specific OnNavigationListener code example is as follows:

Java code:

  1. 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. };

Copy code

The ListContentFragment code is

Java code:

  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 code

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.