Android development-ActionBar combined with Fragment navigation functions (source code sharing)

Source: Internet
Author: User

The tag can be easily explored in the Operation bar and switched between different views of your application. The labels provided by ActionBar are the best choice, as they adapt to different screen sizes. Once you confirm that Fragment appears in the layout, follow these steps: 1. inherit ActionBar. the TabListener interface controls which tag is selected and switches the layout. 2. For each tag you want to add and instantiate an ActionBar, the tab calls setTabListener TabListener (), you can also set the label title and setText () (and an icon setIcon (); 3: addTab ();

The source code of MainActivity is as follows:

Package com. example. f19_fragment07; import android. OS. bundle; import android. app. actionBar; import android. app. actionBar. tab; import android. app. actionBar. tabListener; import android. app. activity; import android. app. fragmentTransaction; public class MainActivity extends Activity implements TabListener {private ActionBar actionBar; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); actionBar = getActionBar (); actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS); // you must have this method to set its attribute actionBar. setDisplayShowTitleEnabled (false); ActionBar. tab tab = actionBar. newTab (). setText ("Information Management "). setIcon (R. drawable. a ). setTabListener (this ). setTag ("1"); ActionBar. tab tab2 = actionBar. newTab (). setText ("motion management "). setIcon (R. drawable. a ). setTabListener (this ). setTag ("2"); ActionBar. tab tab3 = actionBar. newTab (). setText ("System Management "). setIcon (R. drawable. a ). setTabListener (this ). setTag ("3"); actionBar. addTab; actionBar. addTab (tab2); actionBar. addTab (tab3) ;}@ Overridepublic void onTabSelected (Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub // identify by TAG, which tab is called int flag = Integer. parseInt (tab. getTag (). toString (); switch (flag) {case 1: AFragment fragment = new AFragment (); ft. replace (R. id. main, fragment); // you do not need to use the commit Method for submission. Instead, you cannot use the rollback stack mechanism to return data. Here, break is prone to errors; case 2: break; case 3: break ;}} @ Overridepublic void onTabUnselected (Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub} @ Overridepublic void onTabReselected (Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub }}


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.