Java tutorial
/** * Copyright 2014 Scott Weeden-Moody * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.lillicoder.demo.navigationdrawer; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.view.*; import android.widget.AdapterView; import android.widget.ListView; import com.lillicoder.demo.navigationdrawer.navigation.NavigationItem; import com.lillicoder.demo.navigationdrawer.navigation.NavigationListAdapter; import java.util.ArrayList; import java.util.List; /** * Base {@link ActionBarActivity} that provides a navigation drawer. */ public abstract class BaseNavigationActivity extends ActionBarActivity { private DrawerLayout mDrawer; private ListView mNavigationList; private ViewGroup mContentContainer; private ActionBarDrawerToggle mDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_base_navigation); mDrawer = (DrawerLayout) findViewById(R.id.BaseNavigationActivity_drawer); mNavigationList = (ListView) findViewById(R.id.BaseNavigationActivity_navigationList); mContentContainer = (ViewGroup) findViewById(R.id.BaseNavigationActivity_contentContainer); final ActionBar actionBar = getSupportActionBar(); //actionBar.setIcon(R.drawable.ic_launcher); actionBar.setDisplayHomeAsUpEnabled(true); // Configure action bar toggling and navigation drawer look & feel mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, R.drawable.ic_drawer_light, R.string.drawer_open, R.string.drawer_close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getSupportActionBar().setTitle(R.string.drawer_closed_title); supportInvalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); getSupportActionBar().setTitle(R.string.drawer_opened_title); supportInvalidateOptionsMenu(); } }; mDrawer.setDrawerListener(mDrawerToggle); mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); // Configure navigation list List<NavigationItem> navigationItems = getNavigationItems(); NavigationListAdapter adapter = new NavigationListAdapter(navigationItems); mNavigationList.setAdapter(adapter); mNavigationList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { NavigationItem item = (NavigationItem) parent.getItemAtPosition(position); Class<?> targetActivity = item.getTargetActivity(); Context context = parent.getContext(); Intent navigate = new Intent(context, targetActivity); if (position == 0) { // The top level navigation item should clear top navigate.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); } context.startActivity(navigate); } }); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); if (mDrawerToggle != null) { mDrawerToggle.syncState(); } if (mDrawer != null && shouldShowNavigationDrawerOnLaunch()) { // Show the drawer for a first launch scenario (we must manually call the onDrawerOpened callback // as that callback only fires for drag events) mDrawer.openDrawer(Gravity.LEFT); mDrawerToggle.onDrawerOpened(mDrawer); // Disable this behavior for next launch setShowNavigationDrawerOnLaunch(false); } } @Override protected void onStart() { super.onStart(); NavigationListAdapter adapter = (NavigationListAdapter) mNavigationList.getAdapter(); int position = adapter.getPosition(getNavigationItem()); if (position >= 0 && position < adapter.getCount()) { mNavigationList.setItemChecked(position, true); } } @Override public boolean onOptionsItemSelected(MenuItem item) { return mDrawerToggle != null && mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); } @Override public void setContentView(int layoutResID) { // Inflate content layout if (layoutResID > 0) { LayoutInflater inflater = getLayoutInflater(); inflater.inflate(layoutResID, mContentContainer, true); } } /** * Gets the {@link NavigationItem} for this activity. * @return Navigation item for this activity. */ protected abstract NavigationItem getNavigationItem(); /** * Gets the list of {@link NavigationItem} for this activity's navigation drawer. * @return Navigation items for the navigation drawer. */ private List<NavigationItem> getNavigationItems() { List<NavigationItem> items = new ArrayList<NavigationItem>(); items.add(NavigationItem.TOP_LEVEL); items.add(NavigationItem.HOME_AS_UP); items.add(NavigationItem.SIMPLE_ICON); return items; } /** * Enables or disables display of the "hamburger" drawer indicator in the action bar. * @param isEnabled {@code true} to enable the indicator, {@code false} to disable the indicator. */ protected void setDrawerIndicatorEnabled(boolean isEnabled) { if (mDrawerToggle != null) { mDrawerToggle.setDrawerIndicatorEnabled(isEnabled); } } /** * Enables or disables the show navigation drawer on launch behavior for this activity. * @param showNavigationDrawerOnLaunch {@code true} to enable show navigation drawer on launch behavior, * {@code false} to disable the show navigation drawer on launch behavior. */ private void setShowNavigationDrawerOnLaunch(boolean showNavigationDrawerOnLaunch) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = preferences.edit(); String showNavigationOnLaunchKey = getString(R.string.preference_key_show_navigation_drawer_on_launch); editor.putBoolean(showNavigationOnLaunchKey, showNavigationDrawerOnLaunch); editor.commit(); } /** * Determines if this activity should display the navigation drawer after launching. * @return {@code true} if the navigation should be shown on launch, {@code false} otherwise. */ private boolean shouldShowNavigationDrawerOnLaunch() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); String showNavigationOnLaunchKey = getString(R.string.preference_key_show_navigation_drawer_on_launch); return preferences.getBoolean(showNavigationOnLaunchKey, true); } }