Advertisement
Guest User

Untitled

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