Advertisement
Guest User

Untitled

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