Search code examples
androidandroid-fragmentsandroid-tabhostandroid-tabs

Adding tabs again to tab fragment activity in android


I have an app which has navigation type fixed tabs + swipe. And I have created two tabs using two fragments, fragment 1 and fragment 2. It's working fine. Now again I need to create 3 tabs for the fragment 1. How can this be acheived??

This is my MainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;// Tab titles
private String[] tabs = { "Flights","My Bookings" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
        .setTabListener(this));
}

/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
    // on changing the page
    // make respected tab selected
    actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

This is my TabsPagerAdapter class:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int index) {
    // TODO Auto-generated method stub
    switch (index) {
    case 0:
            return new Flight();
    case 1:
            return new Hotel();


    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 2;
}

}

This is my mainactivity.xml file

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

Then there are two fragment activities Flight and Hotel in my app. Now Again I need to add tabs to the fragment activity flight. How can this be acheived?? Can someone please help me out...

Edit: This is an image from net. This is what I exactly want..

enter image description here


Solution

  • your layout of flight fragment must be changed to it:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:descendantFocusability="blocksDescendants" 
     >
    
    <TabHost
    
        android:id="@+id/TabHost01"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0" />
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
               >
    
                <anylayout
                    android:id="@+id/tab_1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                     >
                </anylayout>
    
                <anylayout
                    android:id="@+id/tab_2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:dividerHeight="3dp" >
    
    
                <anylayout
                    android:id="@+id/tab_3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
              </anylayout>
            </FrameLayout>
        </LinearLayout>
     </TabHost>
    
     </LinearLayout>
    

    and in onCreatView :

    mTabHost = (TabHost) v.findViewById(R.id.TabHost01);
    
    mTabHost.setup();
    
    mTabHost.addTab(mTabHost.newTabSpec("tab_1")
                    .setIndicator("View1_Child1")
                    .setContent(R.id.tab_1));
    
    mTabHost.addTab(mTabHost.newTabSpec("tab_2")
                    .setIndicator("View2_Child2")
                    .setContent(R.id.tab_2));