FragmentTabHost from Navigation Drawer

1.1k views Asked by At

I am trying to create an app with the Navigation Drawer using the sample Navigation Drawer app from within Android Studio. My first fragment that I am trying to create from the navigation drawer has 3 tabs and I'm trying to use FragmentTabHost to switch between the tabs and load a slightly different fragment below each tab.I want the user to navigate the main sections of the app from the Navigation Drawer and on some screens I want to have sub-level navigation using TabHost. I basically want to use something like the Two Level navigation that is recommended on the Android Developers site here: https://www.google.com/design/spec/patterns/navigation.html#navigation-two-levels

I found some examples of how to use FragmentTabHost here: http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

The first example doesn't work because the FragmentTabHost is extending FragmentActivity but the Navigation Drawer code appears to only invoke Fragments

Trying to implement the second example using nested fragments is giving me an error the following error:

Error:(106, 17) error: no suitable method found for setup(FragmentActivity,FragmentManager,int) method TabHost.setup(LocalActivityManager) is not applicable (actual and formal argument lists differ in length) method TabHost.setup() is not applicable (actual and formal argument lists differ in length)

at:

mTabHost.setup(getActivity(), getChildFragmentManager(), 
            R.layout.fragment_tab_host);

Here is a snippet from my MainActivity.java:

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks{

    private NavigationDrawerFragment mNavigationDrawerFragment;

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new fragment_tabHost();
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                .replace(R.id.container, fragment).commit();
            // update selected item and title, then close the drawer

        } else {
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
}

Here a snippet of the code for the fragment_tabhost.java which is called when you click the first item in the navigation drawer:

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

public class fragment_tabHost extends Fragment {

    private TabHost mTabHost;    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(),  
            R.layout.fragment_tab_host);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
            tab1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
            tab2.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
            tab3.class, null);

        return mTabHost;

    }
}

I am only targeting a minimum SDK of API level 15 so I am open to a better way to do this if one exists.

1

There are 1 answers

3
swissonid On

This sample app which ships with the android Studio seems to be aged. I would recommend that you use the DrawerLayout. You will find an official sample from google here.

If you want a full example with the newest API take look at this github sample.

For your case the classes, DrawerActivity, TabHolderFragment and TabFragment will be intressting.