// Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)); // Set the list's click listener mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
privateclassDrawerItemClickListenerimplementsListView.OnItemClickListener{ @Override publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id){ selectItem(position); } }
/** Swaps fragments in the main content view */ privatevoidselectItem(int position){ // Create a new fragment and specify the planet to show based on position Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment) .commit();
// Highlight the selected item, update the title, and close the drawer mDrawerList.setItemChecked(position, true); setTitle(mPlanetTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); }
/** Called when a drawer has settled in a completely closed state. */ publicvoidonDrawerClosed(View view){ super.onDrawerClosed(view); getActionBar().setTitle(mTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() }
/** Called when a drawer has settled in a completely open state. */ publicvoidonDrawerOpened(View drawerView){ super.onDrawerOpened(drawerView); getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() } };
// Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle); }
/* Called whenever we call invalidateOptionsMenu() */ @Override publicbooleanonPrepareOptionsMenu(Menu menu){ // If the nav drawer is open, hide action items related to the content view boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); returnsuper.onPrepareOptionsMenu(menu); } }
/** Called when a drawer has settled in a completely closed state. */ publicvoidonDrawerClosed(View view){ super.onDrawerClosed(view); getActionBar().setTitle(mTitle); }
/** Called when a drawer has settled in a completely open state. */ publicvoidonDrawerOpened(View drawerView){ super.onDrawerOpened(drawerView); getActionBar().setTitle(mDrawerTitle); } };
// Set the drawer toggle as the DrawerListener mDrawerLayout.setDrawerListener(mDrawerToggle);
@Override protectedvoidonPostCreate(Bundle savedInstanceState){ super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); }
@Override publicbooleanonOptionsItemSelected(MenuItem item){ // Pass the event to ActionBarDrawerToggle, if it returns // true, then it has handled the app icon touch event if (mDrawerToggle.onOptionsItemSelected(item)) { returntrue; } // Handle your other action bar items...