Wednesday, May 28, 2014

Google Map In Navigation Drawer Fragment - Android

Hey guys, here's my first Android post. Please be gentle. Here's the source code.

This post is to show you how to display a Google Map in a NavigationDrawer Fragment.

I will assume that you have set up Google Play Services, Google Maps API, and your manifest. If you haven't Google is your friend.

I will also assume that you created a NavigationDrawer project. If you haven't File > New Android Project > You'll be able to choose NavigationDrawer.

First create the Map fragment under res/layout
This will automatically draw a map fragment for you.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ccastv2.MainActivity$MFragment"

    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    map:uiZoomControls="true"
 map:uiZoomGestures="false"
 map:cameraTilt="90"
/>

MainActivity.java and NavigationDrawerFragment.java should have been made for you.
NavigationDrawerFragment provides the functionality for the navigation drawer.
MainActivity implements NavigationDrawerCallBacks of NavigationDrawerFragment. It will handle the responses from the navigation drawer.

In the onCreate() method we call setUpFragments to, well, set up the fragments.

private void setUpFragments() {
        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        // If the activity is killed while in BG, it's possible that the
        // fragment still remains in the FragmentManager, so, we don't need to
        // add it again.
        mMapFragment = (MFragment) getSupportFragmentManager().findFragmentByTag(MFragment.TAG);
        if (mMapFragment == null) {
            mMapFragment = MFragment.newInstance();
            ft.add(R.id.container, mMapFragment, MFragment.TAG);
        }
        ft.show(mMapFragment);

        mFriendsFragment = (FriendsFragment) getSupportFragmentManager().findFragmentByTag(FriendsFragment.TAG);
        if (mFriendsFragment == null) {
            mFriendsFragment = FriendsFragment.newInstance("Gary");
            ft.add(R.id.container, mFriendsFragment, FriendsFragment.TAG);
        }
        ft.hide(mFriendsFragment);

        ft.commit();
    }

Lastly, since onDestroy is called when a fragment is detached in its lifecycle, the next time we reattach it onCreate will try to inflate a fragment which is no bueno. だめですよ. 不做.

So we define showFragment() and call it from onNavigationDraweItemSelected().


private void showFragment(Fragment fragmentIn) {
        if (fragmentIn == null) return;

        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

        if (mVisible != null) ft.hide(mVisible);

        ft.show(fragmentIn).commit();
        mVisible = fragmentIn;
    }

No comments:

Post a Comment