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