private void selectItem(int position) { // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); // set selected class mSelectedItem = Constants.DrawerItems.ARRAY[position]; // setTitle(mDrawerTitles[position]); // If drawer isn't locked just close the drawer and // it will move to the selected item by itself (via drawer toggle listener) if (!mIsDrawerLocked) { mDrawerLayout.closeDrawer(mDrawerList); // else move to the selected item yourself } else { callIntentForDrawerItem(mSelectedItem); } }
protected void setupDrawerNavigation(Bundle savedInstanceState) { mDrawerTitle = getString(R.string.app_name); mDrawerLayout = (FixedDrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); ViewGroup viewGroup = (ViewGroup) findViewById(R.id.content_frame); int leftMarginLoaded = ((ViewGroup.MarginLayoutParams) viewGroup.getLayoutParams()).leftMargin; int leftMarginInTablets = (int) getResources().getDimension(R.dimen.drawer_size); int errorInMarginAllowed = 5; // if the left margin of the loaded layout is close to the // one used in tablets then set drawer as open and locked if (Math.abs(leftMarginLoaded - leftMarginInTablets) < errorInMarginAllowed) { mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, mDrawerList); mDrawerLayout.setScrimColor(Color.TRANSPARENT); mIsDrawerLocked = true; } else { // set a custom shadow that overlays the main content when the drawer opens mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mIsDrawerLocked = false; } NavItem mItemIconTexts[] = new NavItem[] { new NavItem("fa-user", getString(R.string.nav_keys), KeyListActivity.class), new NavItem("fa-lock", getString(R.string.nav_encrypt), EncryptActivity.class), new NavItem("fa-unlock", getString(R.string.nav_decrypt), DecryptActivity.class), new NavItem("fa-android", getString(R.string.nav_apps), AppsListActivity.class), }; mDrawerList.setAdapter( new NavigationDrawerAdapter(this, R.layout.drawer_list_item, mItemIconTexts)); mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // enable ActionBar app icon to behave as action to toggle nav drawer // if the drawer is not locked if (!mIsDrawerLocked) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); } // ActionBarDrawerToggle ties together the the proper interactions // between the sliding drawer and the action bar app icon mDrawerToggle = new ActionBarDrawerToggle( this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ R.string.drawer_open, /* "open drawer" description for accessibility */ R.string.drawer_close /* "close drawer" description for accessibility */) { public void onDrawerClosed(View view) { getSupportActionBar().setTitle(mTitle); callIntentForDrawerItem(mSelectedItem); } public void onDrawerOpened(View drawerView) { mTitle = getSupportActionBar().getTitle(); getSupportActionBar().setTitle(mDrawerTitle); // creates call to onPrepareOptionsMenu() supportInvalidateOptionsMenu(); } }; if (!mIsDrawerLocked) { mDrawerLayout.setDrawerListener(mDrawerToggle); } else { // If the drawer is locked open make it un-focusable // so that it doesn't consume all the Back button presses mDrawerLayout.setFocusableInTouchMode(false); } }