Advertisement
rockydcoder

Untitled

Feb 10th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.94 KB | None | 0 0
  1. package com.example.priyanshu.mappr;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.app.Activity;
  5. import android.support.v7.app.ActionBar;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.app.ActionBarDrawerToggle;
  8. import android.support.v4.view.GravityCompat;
  9. import android.support.v4.widget.DrawerLayout;
  10. import android.content.SharedPreferences;
  11. import android.content.res.Configuration;
  12. import android.os.Bundle;
  13. import android.preference.PreferenceManager;
  14. import android.support.v7.widget.Toolbar;
  15. import android.view.LayoutInflater;
  16. import android.view.Menu;
  17. import android.view.MenuInflater;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.AdapterView;
  22. import android.widget.ArrayAdapter;
  23. import android.widget.ListView;
  24. import android.widget.Toast;
  25.  
  26. /**
  27.  * Fragment used for managing interactions for and presentation of a navigation drawer.
  28.  * See the <a href="https://developer.android.com/design/patterns/navigation-drawer.html#Interaction">
  29.  * design guidelines</a> for a complete explanation of the behaviors implemented here.
  30.  */
  31. public class NavigationDrawerFragment extends Fragment {
  32.  
  33.     /**
  34.      * Remember the position of the selected item.
  35.      */
  36.     private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
  37.  
  38.     /**
  39.      * Per the design guidelines, you should show the drawer on launch until the user manually
  40.      * expands it. This shared preference tracks this.
  41.      */
  42.     private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
  43.  
  44.     /**
  45.      * A pointer to the current callbacks instance (the Activity).
  46.      */
  47.     private NavigationDrawerCallbacks mCallbacks;
  48.  
  49.     /**
  50.      * Helper component that ties the action bar to the navigation drawer.
  51.      */
  52.     private ActionBarDrawerToggle mDrawerToggle;
  53.  
  54.     private DrawerLayout mDrawerLayout;
  55.     private ListView mDrawerListView;
  56.     private View mFragmentContainerView;
  57.  
  58.     private int mCurrentSelectedPosition = 0;
  59.     private boolean mFromSavedInstanceState;
  60.     private boolean mUserLearnedDrawer;
  61.  
  62.     private Toolbar toolbar;
  63.  
  64.     public NavigationDrawerFragment() {
  65.     }
  66.  
  67.     @Override
  68.     public void onCreate(Bundle savedInstanceState) {
  69.         super.onCreate(savedInstanceState);
  70.         // Set a toolbar to replace the action bar.
  71.         toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
  72.         // Read in the flag indicating whether or not the user has demonstrated awareness of the
  73.         // drawer. See PREF_USER_LEARNED_DRAWER for details.
  74.         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
  75.         mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
  76.  
  77.         if (savedInstanceState != null) {
  78.             mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
  79.             mFromSavedInstanceState = true;
  80.         }
  81.  
  82.         // Select either the default item (0) or the last selected item.
  83.         selectItem(mCurrentSelectedPosition);
  84.     }
  85.  
  86.     @Override
  87.     public void onActivityCreated(Bundle savedInstanceState) {
  88.         super.onActivityCreated(savedInstanceState);
  89.         // Indicate that this fragment would like to influence the set of actions in the action bar.
  90.         setHasOptionsMenu(true);
  91.     }
  92.  
  93.     @Override
  94.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  95.                              Bundle savedInstanceState) {
  96.         mDrawerListView = (ListView) inflater.inflate(
  97.                 R.layout.fragment_navigation_drawer, container, false);
  98.         mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  99.             @Override
  100.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  101.                 selectItem(position);
  102.             }
  103.         });
  104.         mDrawerListView.setAdapter(new ArrayAdapter<String>(
  105.                 toolbar.getContext(),
  106.                 android.R.layout.simple_list_item_activated_1,
  107.                 android.R.id.text1,
  108.                 new String[]{
  109.                         getString(R.string.title_section1),
  110.                         getString(R.string.title_section2),
  111.                         getString(R.string.title_section3),
  112.                 }));
  113.         mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
  114.         return mDrawerListView;
  115.     }
  116.  
  117.     public boolean isDrawerOpen() {
  118.         return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
  119.     }
  120.  
  121.     /**
  122.      * Users of this fragment must call this method to set up the navigation drawer interactions.
  123.      *
  124.      * @param fragmentId   The android:id of this fragment in its activity's layout.
  125.      * @param drawerLayout The DrawerLayout containing this fragment's UI.
  126.      */
  127.     public void setUp(int fragmentId, DrawerLayout drawerLayout) {
  128.         mFragmentContainerView = getActivity().findViewById(fragmentId);
  129.         mDrawerLayout = drawerLayout;
  130.  
  131.         // set a custom shadow that overlays the main content when the drawer opens
  132.         mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
  133.         // set up the drawer's list view with items and click listener
  134.  
  135.         ActionBar actionBar = getActionBar();
  136.         actionBar.setDisplayHomeAsUpEnabled(true);
  137.         actionBar.setHomeButtonEnabled(true);
  138.  
  139.         // ActionBarDrawerToggle ties together the the proper interactions
  140.         // between the navigation drawer and the action bar app icon.
  141.         mDrawerToggle = new ActionBarDrawerToggle(
  142.                 getActivity(),                    /* host Activity */
  143.                 mDrawerLayout,                    /* DrawerLayout object */
  144.                 R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
  145.                 R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
  146.                 R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
  147.         ) {
  148.             @Override
  149.             public void onDrawerClosed(View drawerView) {
  150.                 super.onDrawerClosed(drawerView);
  151.                 if (!isAdded()) {
  152.                     return;
  153.                 }
  154.  
  155.                 getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
  156.             }
  157.  
  158.             @Override
  159.             public void onDrawerOpened(View drawerView) {
  160.                 super.onDrawerOpened(drawerView);
  161.                 if (!isAdded()) {
  162.                     return;
  163.                 }
  164.  
  165.                 if (!mUserLearnedDrawer) {
  166.                     // The user manually opened the drawer; store this flag to prevent auto-showing
  167.                     // the navigation drawer automatically in the future.
  168.                     mUserLearnedDrawer = true;
  169.                     SharedPreferences sp = PreferenceManager
  170.                             .getDefaultSharedPreferences(getActivity());
  171.                     sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
  172.                 }
  173.  
  174.                 getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
  175.             }
  176.         };
  177.  
  178.         // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
  179.         // per the navigation drawer design guidelines.
  180.         if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
  181.             mDrawerLayout.openDrawer(mFragmentContainerView);
  182.         }
  183.  
  184.         // Defer code dependent on restoration of previous instance state.
  185.         mDrawerLayout.post(new Runnable() {
  186.             @Override
  187.             public void run() {
  188.                 mDrawerToggle.syncState();
  189.             }
  190.         });
  191.  
  192.         mDrawerLayout.setDrawerListener(mDrawerToggle);
  193.     }
  194.  
  195.     private void selectItem(int position) {
  196.         mCurrentSelectedPosition = position;
  197.         if (mDrawerListView != null) {
  198.             mDrawerListView.setItemChecked(position, true);
  199.         }
  200.         if (mDrawerLayout != null) {
  201.             mDrawerLayout.closeDrawer(mFragmentContainerView);
  202.         }
  203.         if (mCallbacks != null) {
  204.             mCallbacks.onNavigationDrawerItemSelected(position);
  205.         }
  206.     }
  207.  
  208.     @Override
  209.     public void onAttach(Activity activity) {
  210.         super.onAttach(activity);
  211.         try {
  212.             mCallbacks = (NavigationDrawerCallbacks) activity;
  213.         } catch (ClassCastException e) {
  214.             throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
  215.         }
  216.     }
  217.  
  218.     @Override
  219.     public void onDetach() {
  220.         super.onDetach();
  221.         mCallbacks = null;
  222.     }
  223.  
  224.     @Override
  225.     public void onSaveInstanceState(Bundle outState) {
  226.         super.onSaveInstanceState(outState);
  227.         outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
  228.     }
  229.  
  230.     @Override
  231.     public void onConfigurationChanged(Configuration newConfig) {
  232.         super.onConfigurationChanged(newConfig);
  233.         // Forward the new configuration the drawer toggle component.
  234.         mDrawerToggle.onConfigurationChanged(newConfig);
  235.     }
  236.  
  237.     @Override
  238.     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  239.         // If the drawer is open, show the global app actions in the action bar. See also
  240.         // showGlobalContextActionBar, which controls the top-left area of the action bar.
  241.         if (mDrawerLayout != null && isDrawerOpen()) {
  242.             inflater.inflate(R.menu.global, menu);
  243.             showGlobalContextActionBar();
  244.         }
  245.         super.onCreateOptionsMenu(menu, inflater);
  246.     }
  247.  
  248.     @Override
  249.     public boolean onOptionsItemSelected(MenuItem item) {
  250.         if (mDrawerToggle.onOptionsItemSelected(item)) {
  251.             return true;
  252.         }
  253.  
  254.         if (item.getItemId() == R.id.action_example) {
  255.             Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show();
  256.             return true;
  257.         }
  258.  
  259.         return super.onOptionsItemSelected(item);
  260.     }
  261.  
  262.     /**
  263.      * Per the navigation drawer design guidelines, updates the action bar to show the global app
  264.      * 'context', rather than just what's in the current screen.
  265.      */
  266.     private void showGlobalContextActionBar() {
  267.         ActionBar actionBar = getActionBar();
  268.         actionBar.setDisplayShowTitleEnabled(true);
  269.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  270.         actionBar.setTitle(R.string.app_name);
  271.     }
  272.  
  273.     private ActionBar getActionBar() {
  274.         return ((ActionBarActivity) getActivity()).getSupportActionBar();
  275.     }
  276.  
  277.     /**
  278.      * Callbacks interface that all activities using this fragment must implement.
  279.      */
  280.     public static interface NavigationDrawerCallbacks {
  281.         /**
  282.          * Called when an item in the navigation drawer is selected.
  283.          */
  284.         void onNavigationDrawerItemSelected(int position);
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement