Use Demo2 in combination with Android actionBar and Fragment

Source: Internet
Author: User

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

Related Article

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.