|
| 1 | +package com.saulmm.material; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | + |
| 5 | +import android.app.ActionBar; |
| 6 | +import android.app.Fragment; |
| 7 | +import android.app.FragmentManager; |
| 8 | +import android.content.Context; |
| 9 | +import android.os.Build; |
| 10 | +import android.os.Bundle; |
| 11 | +import android.view.Gravity; |
| 12 | +import android.view.LayoutInflater; |
| 13 | +import android.view.Menu; |
| 14 | +import android.view.MenuItem; |
| 15 | +import android.view.View; |
| 16 | +import android.view.ViewGroup; |
| 17 | +import android.support.v4.widget.DrawerLayout; |
| 18 | +import android.widget.ArrayAdapter; |
| 19 | +import android.widget.TextView; |
| 20 | +import com.saulmm.material.R; |
| 21 | + |
| 22 | +public class NavDrawerActivity extends Activity |
| 23 | + implements NavigationDrawerFragment.NavigationDrawerCallbacks { |
| 24 | + |
| 25 | + /** |
| 26 | + * Fragment managing the behaviors, interactions and presentation of the navigation drawer. |
| 27 | + */ |
| 28 | + private NavigationDrawerFragment mNavigationDrawerFragment; |
| 29 | + |
| 30 | + /** |
| 31 | + * Used to store the last screen title. For use in {@link #restoreActionBar()}. |
| 32 | + */ |
| 33 | + private CharSequence mTitle; |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + setContentView(R.layout.activity_nav_drawer); |
| 39 | + |
| 40 | + mNavigationDrawerFragment = (NavigationDrawerFragment) |
| 41 | + getFragmentManager().findFragmentById(R.id.navigation_drawer); |
| 42 | + mTitle = getTitle(); |
| 43 | + |
| 44 | + // Set up the drawer. |
| 45 | + mNavigationDrawerFragment.setUp( |
| 46 | + R.id.navigation_drawer, |
| 47 | + (DrawerLayout) findViewById(R.id.drawer_layout)); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onNavigationDrawerItemSelected(int position) { |
| 52 | + // update the main content by replacing fragments |
| 53 | + FragmentManager fragmentManager = getFragmentManager(); |
| 54 | + fragmentManager.beginTransaction() |
| 55 | + .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) |
| 56 | + .commit(); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + public void restoreActionBar() { |
| 62 | + ActionBar actionBar = getActionBar(); |
| 63 | + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 64 | + actionBar.setDisplayShowTitleEnabled(true); |
| 65 | + actionBar.setTitle(mTitle); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 73 | + // Handle action bar item clicks here. The action bar will |
| 74 | + // automatically handle clicks on the Home/Up button, so long |
| 75 | + // as you specify a parent activity in AndroidManifest.xml. |
| 76 | + int id = item.getItemId(); |
| 77 | + if (id == R.id.action_settings) { |
| 78 | + return true; |
| 79 | + } |
| 80 | + return super.onOptionsItemSelected(item); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * A placeholder fragment containing a simple view. |
| 85 | + */ |
| 86 | + public static class PlaceholderFragment extends Fragment { |
| 87 | + /** |
| 88 | + * The fragment argument representing the section number for this |
| 89 | + * fragment. |
| 90 | + */ |
| 91 | + private static final String ARG_SECTION_NUMBER = "section_number"; |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns a new instance of this fragment for the given section |
| 95 | + * number. |
| 96 | + */ |
| 97 | + public static PlaceholderFragment newInstance(int sectionNumber) { |
| 98 | + PlaceholderFragment fragment = new PlaceholderFragment(); |
| 99 | + Bundle args = new Bundle(); |
| 100 | + args.putInt(ARG_SECTION_NUMBER, sectionNumber); |
| 101 | + fragment.setArguments(args); |
| 102 | + return fragment; |
| 103 | + } |
| 104 | + |
| 105 | + public PlaceholderFragment() { |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 110 | + Bundle savedInstanceState) { |
| 111 | + View rootView = inflater.inflate(R.layout.fragment_nav_drawer, container, false); |
| 112 | + return rootView; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments