Advertisement
rockydcoder

Untitled

Feb 10th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. package com.example.priyanshu.mappr;
  2.  
  3. import android.app.Activity;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.support.v7.app.ActionBar;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.app.FragmentManager;
  8. import android.content.Context;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.support.v7.widget.Toolbar;
  12. import android.view.Gravity;
  13. import android.view.LayoutInflater;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.support.v4.widget.DrawerLayout;
  19. import android.widget.ArrayAdapter;
  20. import android.widget.TextView;
  21.  
  22.  
  23. public class HomePage extends ActionBarActivity
  24.         implements NavigationDrawerFragment.NavigationDrawerCallbacks {
  25.  
  26.     /**
  27.      * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
  28.      */
  29.     private NavigationDrawerFragment mNavigationDrawerFragment;
  30.  
  31.     /**
  32.      * Used to store the last screen title. For use in {@link #restoreActionBar()}.
  33.      */
  34.     private CharSequence mTitle;
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.activity_home_page);
  40.  
  41.         // Set a toolbar to replace the action bar.
  42.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  43.         setSupportActionBar(toolbar);
  44.  
  45.         mNavigationDrawerFragment = (NavigationDrawerFragment)
  46.                 getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
  47.         mTitle = getTitle();
  48.  
  49.         // Set up the drawer.
  50.         mNavigationDrawerFragment.setUp(
  51.                 R.id.navigation_drawer,
  52.                 (DrawerLayout) findViewById(R.id.drawer_layout));
  53.     }
  54.  
  55.     @Override
  56.     public void onNavigationDrawerItemSelected(int position) {
  57.         // update the main content by replacing fragments
  58.         FragmentManager fragmentManager = getSupportFragmentManager();
  59.         fragmentManager.beginTransaction()
  60.                 .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
  61.                 .commit();
  62.     }
  63.  
  64.     public void onSectionAttached(int number) {
  65.         switch (number) {
  66.             case 1:
  67.                 mTitle = getString(R.string.title_section1);
  68.                 break;
  69.             case 2:
  70.                 mTitle = getString(R.string.title_section2);
  71.                 break;
  72.             case 3:
  73.                 mTitle = getString(R.string.title_section3);
  74.                 break;
  75.         }
  76.     }
  77.  
  78.     public void restoreActionBar() {
  79.         ActionBar actionBar = getSupportActionBar();
  80.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
  81.         actionBar.setDisplayShowTitleEnabled(true);
  82.         actionBar.setTitle(mTitle);
  83.     }
  84.  
  85.  
  86.     @Override
  87.     public boolean onCreateOptionsMenu(Menu menu) {
  88.         if (!mNavigationDrawerFragment.isDrawerOpen()) {
  89.             // Only show items in the action bar relevant to this screen
  90.             // if the drawer is not showing. Otherwise, let the drawer
  91.             // decide what to show in the action bar.
  92.             getMenuInflater().inflate(R.menu.home_page, menu);
  93.             restoreActionBar();
  94.             return true;
  95.         }
  96.         return super.onCreateOptionsMenu(menu);
  97.     }
  98.  
  99.     @Override
  100.     public boolean onOptionsItemSelected(MenuItem item) {
  101.         // Handle action bar item clicks here. The action bar will
  102.         // automatically handle clicks on the Home/Up button, so long
  103.         // as you specify a parent activity in AndroidManifest.xml.
  104.         int id = item.getItemId();
  105.  
  106.         //noinspection SimplifiableIfStatement
  107.         if (id == R.id.action_settings) {
  108.             return true;
  109.         }
  110.  
  111.         return super.onOptionsItemSelected(item);
  112.     }
  113.  
  114.     /**
  115.      * A placeholder fragment containing a simple view.
  116.      */
  117.     public static class PlaceholderFragment extends Fragment {
  118.         /**
  119.          * The fragment argument representing the section number for this
  120.          * fragment.
  121.          */
  122.         private static final String ARG_SECTION_NUMBER = "section_number";
  123.  
  124.         /**
  125.          * Returns a new instance of this fragment for the given section
  126.          * number.
  127.          */
  128.         public static PlaceholderFragment newInstance(int sectionNumber) {
  129.             PlaceholderFragment fragment = new PlaceholderFragment();
  130.             Bundle args = new Bundle();
  131.             args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  132.             fragment.setArguments(args);
  133.             return fragment;
  134.         }
  135.  
  136.         public PlaceholderFragment() {
  137.         }
  138.  
  139.         @Override
  140.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  141.                                  Bundle savedInstanceState) {
  142.             View rootView = inflater.inflate(R.layout.fragment_home_page, container, false);
  143.             return rootView;
  144.         }
  145.  
  146.         @Override
  147.         public void onAttach(Activity activity) {
  148.             super.onAttach(activity);
  149.             ((HomePage) activity).onSectionAttached(
  150.                     getArguments().getInt(ARG_SECTION_NUMBER));
  151.         }
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement