Android Open Source - FadingActionBar Navigation Drawer Activity






From Project

Back to project page FadingActionBar.

License

The source code is released under:

Apache License

If you think the Android project FadingActionBar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 Antonio Leiva/*w  w w. j a v a  2  s .  c o  m*/
 * Copyright (C) 2013 Manuel Peinado
 *
 * 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.manuelpeinado.fadingactionbar.demo;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NavigationDrawerActivity extends Activity implements AdapterView.OnItemClickListener {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mCityNames;
    private int[] mCityImages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_drawer);

        mTitle = mDrawerTitle = getTitle();
        mCityNames = getResources().getStringArray(R.array.drawer_items);
        TypedArray typedArray = getResources().obtainTypedArray(R.array.city_images);
        mCityImages = new int [typedArray.length()];
        for (int i = 0; i < typedArray.length(); ++i) {  
            mCityImages[i] = typedArray.getResourceId(i, 0);
        }
        typedArray.recycle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mCityNames));
        mDrawerList.setOnItemClickListener(this);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().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) {
                getActionBar().setTitle(mTitle);
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments
        Fragment fragment = new SampleFragment();
        Bundle args = new Bundle();
        args.putInt(SampleFragment.ARG_IMAGE_RES, mCityImages[position]);
        args.putInt(SampleFragment.ARG_ACTION_BG_RES, R.drawable.ab_background);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mCityNames[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }
}




Java Source Code List

com.manuelpeinado.fadingactionbar.FadingActionBarHelperBase.java
com.manuelpeinado.fadingactionbar.FadingActionBarHelper.java
com.manuelpeinado.fadingactionbar.Utils.java
com.manuelpeinado.fadingactionbar.demo.ActivityInfo.java
com.manuelpeinado.fadingactionbar.demo.HeaderOverlayActivity.java
com.manuelpeinado.fadingactionbar.demo.HomeActivity.java
com.manuelpeinado.fadingactionbar.demo.LightActionBarActivity.java
com.manuelpeinado.fadingactionbar.demo.LightBackgroundActivity.java
com.manuelpeinado.fadingactionbar.demo.ListViewActivity.java
com.manuelpeinado.fadingactionbar.demo.NavigationDrawerActivity.java
com.manuelpeinado.fadingactionbar.demo.NoParallaxActivity.java
com.manuelpeinado.fadingactionbar.demo.SampleFragmentActivity.java
com.manuelpeinado.fadingactionbar.demo.SampleFragment.java
com.manuelpeinado.fadingactionbar.demo.ScrollViewActivity.java
com.manuelpeinado.fadingactionbar.demo.ShortContentActivity.java
com.manuelpeinado.fadingactionbar.demo.WebViewActivity.java
com.manuelpeinado.fadingactionbar.extras.actionbarcompat.FadingActionBarHelper.java
com.manuelpeinado.fadingactionbar.extras.actionbarsherlock.FadingActionBarHelper.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.ActivityInfo.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.HeaderOverlayActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.HomeActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.LightActionBarActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.LightBackgroundActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.ListViewActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.NoParallaxActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.SampleFragmentActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.SampleFragment.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.ScrollViewActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarcompat.WebViewActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.ActivityInfo.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.HeaderOverlayActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.HomeActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.LightActionBarActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.LightBackgroundActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.ListViewActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.NoParallaxActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.SampleFragmentActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.SampleFragment.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.ScrollViewActivity.java
com.manuelpeinado.fadingactionbar.samples.actionbarsherlock.WebViewActivity.java
com.manuelpeinado.fadingactionbar.view.ObservableScrollView.java
com.manuelpeinado.fadingactionbar.view.ObservableScrollable.java
com.manuelpeinado.fadingactionbar.view.ObservableWebViewWithHeader.java
com.manuelpeinado.fadingactionbar.view.OnScrollChangedCallback.java
com.manuelpeinado.fadingactionbar.view.RootLayout.java