Back to project page NestedFragment.
The source code is released under:
GNU General Public License
If you think the Android project NestedFragment listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.sams.nestedfragment; /* ww w . j a va 2 s . c o m*/ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getSupportActionBar(); // Specify that tabs should be displayed in the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // Create a tab listener that is called when the user changes tabs. ActionBar.TabListener tabListener = new ActionBar.TabListener() { public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) { final Fragment firstTabFragment = new FirstTabFragment(); final Fragment secondTabFragment = new SecondTabFragment(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); if(tab.getPosition() == 0) fragmentTransaction.replace(R.id.ll_fragmentLayout, firstTabFragment); else fragmentTransaction.replace(R.id.ll_fragmentLayout, secondTabFragment); // Commit the transaction fragmentTransaction.commit(); } public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) { // hide the given tab } public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) { // probably ignore this event } }; actionBar.addTab(actionBar.newTab().setText("TAB 1").setTabListener(tabListener)); actionBar.addTab(actionBar.newTab().setText("TAB 2").setTabListener(tabListener)); } }