Samir Abis
Samir Abis

Reputation: 13

Android GoogleMap (API V2) and Tabs

I want a program that should contain a view with 4 tabs and each one has a different things (Example: 1° tab = map, 2° tab = text etc etc). I'm a bit newb about those projects, and I'm on a position where I've successfully imported and started the app with a map. If you think it's better using something else than Tabs, I'm agreeing with you since I can't find anything about.

Upvotes: 0

Views: 295

Answers (1)

jwong
jwong

Reputation: 76

If you have anything that has more than three or so tabs, navigation tabs are probably not an ideal choice.

You can try sideNavigationMenu or some other sliding menu library. I find that sideNavigationMenu is the easiest library to work with if you're just starting out.

Regardless, if you would like to continue with navigation tabs, below is some sample code to help you get started.

public class MainActivity extends Activity{

public static Context appContext;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    appContext = getApplicationContext();


   //ActionBar
    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    bar.addTab(bar.newTab()
            .setText("Map Fragment")
            .setTabListener(new TabListener<MapSampleFragment>(
                    this, "map_fragment", MapSampleFragment.class)));

    bar.addTab(bar.newTab()
            .setText("List Fragment")
            .setTabListener(new TabListener<LocationListFragment>(
                    this, "list_fragment", LocationListFragment.class)));



    bar.addTab(bar.newTab()
            .setText("Other Fragment")
            .setTabListener(new TabListener<MoreStuffFragment>(
                    this, "more_stuff_fragment", MoreStuffFragment.class)));  

}

You would then need a write your TabListener to handle switching between fragments. Since you're working with Google Maps Android, it's better to hide your fragments and show them when you need to as oppose to detaching and attaching fragments.

This is pretty much the code taken from the API Demos with the exception of using .show() and .hide()

public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;

    public TabListener(Activity activity, String tag, Class<T> clz) {
        this(activity, tag, clz, null);
    }

    public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
            ft.hide(mFragment);
            ft.commit();
        }
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            ft.show(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.hide(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Related Questions