The previous article introduced the use of ActionBar. Here we introduce another method of using ActionBar, which achieves the same effect as the previous GroupActivity or TabHost and can be used as a navigation bar.
Implementation:
Source code:
Layout file: activity_main:
F1.xml (layout file for Sports News ):
F2.xml (Entertainment News ):
F3.xml (Military News ):
Code file:
MainActivity:
Package com. fragmentdemo10_actionbar; import android. app. actionBar; import android. app. actionBar. tab; import android. app. actionBar. tabListener; import android. app. activity; import android. app. fragmentTransaction; import android. OS. bundle; public class MainActivity extends Activity {private ActionBar actionBar;/*** set three Integer constants, respectively, 0, 1, and 2, corresponding to sports news, entertainment news, and Military News. */Private final int SPORTS = 0; private final int ENTERTAINMENT = 1; private final int MILITARY = 2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); actionBar = getActionBar (); // sets the ActionBar navigation mode. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS); actionBar. setDisplayOptions (0, ActionBar. DISPLAY_SHOW_TITLE ); /*** Add three tabs: sports news, entertainment news, and Military News. */ActionBar. addTab (actionBar. newTab (). setText ("Sports News "). setIcon (R. drawable. ic_launcher ). setTabListener (new MyTabListener ()). setTag (SPORTS); actionBar. addTab (actionBar. newTab (). setText ("Entertainment News "). setIcon (R. drawable. ic_launcher ). setTabListener (new MyTabListener ()). setTag (ENTERTAINMENT); actionBar. addTab (actionBar. newTab (). setText ("Military News "). setIcon (R. drawable. ic_launcher ). setTabListener (new MyTabListener ()). setTag (MILITARY);} class MyTabListener implements TabListener {@ Overridepublic void onTabSelected (Tab tab, FragmentTransaction ft) {switch (Integer. parseInt (tab. getTag (). toString () {/*** SPORTS news */case SPORTS: ft. replace (R. id. main, new FragementA (); break;/*** ENTERTAINMENT news */case ENTERTAINMENT: ft. replace (R. id. main, new FragementB (); break;/*** */case MILITARY: ft. replace (R. id. main, new FragementC (); break; default: break; }}@ Overridepublic void onTabUnselected (Tab tab, FragmentTransaction ft) {}@ Overridepublic void onTabReselected (Tab, FragmentTransaction ft) {}}}
FragmentA (Fragment corresponding to Tab Sports News ):
Package com. fragmentdemo10_actionbar; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup;/*** Fragment corresponding to Tab sports news **/public class FragementA extends Fragment {@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. f1, null); return view ;}}
FragmentB (Fragment corresponding to Tab Entertainment News ):
Package com. fragmentdemo10_actionbar; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup;/*** Fragment corresponding to TAB entertainment news **/public class FragementB extends Fragment {@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. f2, null); return view ;}}
FragmentC (Fragment corresponding to Tab Military News ):
Package com. fragmentdemo10_actionbar; import android. app. fragment; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup;/*** Fragment corresponding to TAB Military News **/public class FragementC extends Fragment {@ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater. inflate (R. layout. f3, null); return view ;}}
Source code download:
Click to download source code