Java tutorial
/* * Copyright (C) 2015 A Weyoung App * * 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 org.weyoung.xianbicycle; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ScrollView; import android.widget.TextView; import com.github.amlcurran.showcaseview.ShowcaseView; import org.weyoung.xianbicycle.utils.CoachUtil; import java.util.ArrayList; /** * Copyright 2014 REA Group Ltd */ public class BaseActivity extends AppCompatActivity { private static final String TAG = BaseActivity.class.getSimpleName(); private Handler mHandler; private DrawerLayout mDrawerLayout; protected static final int NAVDRAWER_ITEM_ABOUT = 0; protected static final int NAVDRAWER_ITEM_SEARCH = 1; protected static final int NAVDRAWER_ITEM_BOOKMARK = 2; protected static final int NAVDRAWER_ITEM_INVALID = -1; protected static final int NAVDRAWER_ITEM_SEPARATOR = -2; private static final int NAVDRAWER_ITEM_DEFAULT = NAVDRAWER_ITEM_SEARCH; private int currentItem = NAVDRAWER_ITEM_DEFAULT; private static final int[] NAVDRAWER_TITLE_RES_ID = new int[] { R.string.navdrawer_item_about, R.string.navdrawer_item_search, R.string.navdrawer_item_bookmark, }; private static final int[] NAVDRAWER_ICON_RES_ID = new int[] { R.drawable.ic_action_about, R.drawable.ic_action_search, R.drawable.ic_action_bookmark, }; // delay to launch nav drawer item, to allow close animation to play private static final int NAVDRAWER_LAUNCH_DELAY = 250; // fade in and fade out durations for the main content when switching between // different Activities of the app through the Nav Drawer private static final int MAIN_CONTENT_FADEOUT_DURATION = 150; private static final int MAIN_CONTENT_FADEIN_DURATION = 250; // list of navdrawer items that were actually added to the navdrawer, in order private ArrayList<Integer> mNavDrawerItems = new ArrayList<>(); // views that correspond to each navdrawer item, null if not yet created private View[] mNavDrawerItemViews = null; private ViewGroup mDrawerItemsListContainer; // A Runnable that we should execute when the navigation drawer finishes its closing animation private Runnable mDeferredOnDrawerClosedRunnable; // Primary toolbar and drawer toggle private Toolbar mActionBarToolbar; private ShowcaseView showcaseView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(); ActionBar ab = getSupportActionBar(); if (ab != null) { ab.setDisplayHomeAsUpEnabled(true); } } @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); getActionBarToolbar(); showcaseView = CoachUtil.showCoachMarkIfNecessary(this, getActionBarToolbar()); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); setupNavDrawer(); } protected Toolbar getActionBarToolbar() { if (mActionBarToolbar == null) { mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); if (mActionBarToolbar != null) { setSupportActionBar(mActionBarToolbar); } } return mActionBarToolbar; } //================================================================================ // Navigation Drawer //================================================================================ private void setupNavDrawer() { // What nav drawer item should be selected? int selfItem = getCurrentNavDrawerItem(); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); if (mDrawerLayout == null) { return; } mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); ScrollView navDrawer = (ScrollView) mDrawerLayout.findViewById(R.id.navdrawer); if (selfItem == NAVDRAWER_ITEM_INVALID) { // do not show a nav drawer if (navDrawer != null) { ((ViewGroup) navDrawer.getParent()).removeView(navDrawer); } mDrawerLayout = null; return; } if (mActionBarToolbar != null) { mActionBarToolbar.setNavigationIcon(R.drawable.ic_drawer); mActionBarToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mDrawerLayout.openDrawer(Gravity.START); } }); } mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerClosed(View drawerView) { // run deferred action, if we have one if (mDeferredOnDrawerClosedRunnable != null) { mDeferredOnDrawerClosedRunnable.run(); mDeferredOnDrawerClosedRunnable = null; } onNavDrawerStateChanged(false, false); } @Override public void onDrawerOpened(View drawerView) { if (showcaseView != null && showcaseView.isShowing()) { showcaseView.hide(); } onNavDrawerStateChanged(true, false); } @Override public void onDrawerStateChanged(int newState) { onNavDrawerStateChanged(isNavDrawerOpen(), newState != DrawerLayout.STATE_IDLE); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { onNavDrawerSlide(slideOffset); } }); mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.START); // populate the nav drawer with the correct items populateNavDrawer(); goToNavDrawerItem(NAVDRAWER_ITEM_DEFAULT); } private void populateNavDrawer() { mNavDrawerItems.clear(); mNavDrawerItems.add(NAVDRAWER_ITEM_ABOUT); mNavDrawerItems.add(NAVDRAWER_ITEM_SEPARATOR); // Other items that are always in the nav drawer irrespective of whether the // attendee is on-site or remote: mNavDrawerItems.add(NAVDRAWER_ITEM_SEARCH); mNavDrawerItems.add(NAVDRAWER_ITEM_BOOKMARK); createNavDrawerItems(); } private void createNavDrawerItems() { mDrawerItemsListContainer = (ViewGroup) findViewById(R.id.navdrawer_items_list); if (mDrawerItemsListContainer == null) { return; } mNavDrawerItemViews = new View[mNavDrawerItems.size()]; mDrawerItemsListContainer.removeAllViews(); int i = 0; for (int itemId : mNavDrawerItems) { mNavDrawerItemViews[i] = makeNavDrawerItem(itemId, mDrawerItemsListContainer); mDrawerItemsListContainer.addView(mNavDrawerItemViews[i]); ++i; } } private View makeNavDrawerItem(final int itemId, ViewGroup container) { boolean selected = getCurrentNavDrawerItem() == itemId; int layoutToInflate; if (itemId == NAVDRAWER_ITEM_SEPARATOR) { layoutToInflate = R.layout.navdrawer_separator; } else { layoutToInflate = R.layout.navdrawer_item; } View view = getLayoutInflater().inflate(layoutToInflate, container, false); if (itemId == NAVDRAWER_ITEM_SEPARATOR) { // we are done return view; } ImageView iconView = (ImageView) view.findViewById(R.id.icon); TextView titleView = (TextView) view.findViewById(R.id.title); int iconId = itemId >= 0 && itemId < NAVDRAWER_ICON_RES_ID.length ? NAVDRAWER_ICON_RES_ID[itemId] : 0; int titleId = itemId >= 0 && itemId < NAVDRAWER_TITLE_RES_ID.length ? NAVDRAWER_TITLE_RES_ID[itemId] : 0; // set icon and text iconView.setVisibility(iconId > 0 ? View.VISIBLE : View.GONE); if (iconId > 0) { iconView.setImageResource(iconId); } titleView.setText(getString(titleId)); formatNavDrawerItem(view, itemId, selected); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onNavDrawerItemClicked(itemId); } }); return view; } protected boolean isNavDrawerOpen() { return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(Gravity.START); } protected void onNavDrawerStateChanged(boolean isOpen, boolean isAnimating) { } protected void onNavDrawerSlide(float offset) { } private void formatNavDrawerItem(View view, int itemId, boolean selected) { if (itemId == NAVDRAWER_ITEM_SEPARATOR) { // not applicable return; } ImageView iconView = (ImageView) view.findViewById(R.id.icon); TextView titleView = (TextView) view.findViewById(R.id.title); // configure its appearance according to whether or not it's selected titleView.setTextColor(selected ? getResources().getColor(R.color.theme_primary) : getResources().getColor(R.color.real_dark)); iconView.setColorFilter(selected ? getResources().getColor(R.color.theme_primary) : getResources().getColor(R.color.real_dark)); } private void onNavDrawerItemClicked(final int itemId) { if (itemId == getCurrentNavDrawerItem()) { mDrawerLayout.closeDrawer(Gravity.START); return; } currentItem = itemId; // launch the target Activity after a short delay, to allow the close animation to play mHandler.postDelayed(new Runnable() { @Override public void run() { goToNavDrawerItem(itemId); } }, NAVDRAWER_LAUNCH_DELAY); // change the active item on the list so the user can see the item changed setSelectedNavDrawerItem(itemId); mDrawerLayout.closeDrawer(Gravity.START); } protected void goToNavDrawerItem(int item) { } private void setSelectedNavDrawerItem(int itemId) { if (mNavDrawerItemViews != null) { for (int i = 0; i < mNavDrawerItemViews.length; i++) { if (i < mNavDrawerItems.size()) { int thisItemId = mNavDrawerItems.get(i); formatNavDrawerItem(mNavDrawerItemViews[i], thisItemId, itemId == thisItemId); } } } } protected int getCurrentNavDrawerItem() { return currentItem; } }