Advertisement
Guest User

Untitled

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