Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.supportv7.app;
     17 
     18 import com.example.android.supportv7.Shakespeare;
     19 
     20 import android.support.v7.app.ActionBarActivity;
     21 import android.content.res.Configuration;
     22 import android.os.Bundle;
     23 import android.support.v4.view.GravityCompat;
     24 import android.support.v4.widget.DrawerLayout;
     25 import android.support.v7.app.ActionBar;
     26 import android.support.v7.app.ActionBarDrawerToggle;
     27 import android.view.MenuItem;
     28 import android.view.View;
     29 import android.widget.AdapterView;
     30 import android.widget.ArrayAdapter;
     31 import android.widget.ListView;
     32 import android.widget.TextView;
     33 import com.example.android.supportv7.R;
     34 
     35 public class ActionBarWithDrawerLayout extends ActionBarActivity {
     36     private DrawerLayout mDrawerLayout;
     37     private ListView mDrawer;
     38     private TextView mContent;
     39 
     40     private ActionBarHelper mActionBar;
     41 
     42     private ActionBarDrawerToggle mDrawerToggle;
     43 
     44     @Override
     45     protected void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         setContentView(R.layout.action_bar_drawer_layout);
     48         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
     49         mDrawer = (ListView) findViewById(R.id.start_drawer);
     50         mContent = (TextView) findViewById(R.id.content_text);
     51 
     52         mDrawerLayout.setDrawerListener(new DemoDrawerListener());
     53 
     54         // The drawer title must be set in order to announce state changes when
     55         // accessibility is turned on. This is typically a simple description,
     56         // e.g. "Navigation".
     57         mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.drawer_title));
     58 
     59         mDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
     60                 Shakespeare.TITLES));
     61         mDrawer.setOnItemClickListener(new DrawerItemClickListener());
     62 
     63         mActionBar = createActionBarHelper();
     64         mActionBar.init();
     65 
     66         // ActionBarDrawerToggle provides convenient helpers for tying together the
     67         // prescribed interactions between a top-level sliding drawer and the action bar.
     68         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
     69                 R.string.drawer_open, R.string.drawer_close);
     70     }
     71 
     72     @Override
     73     protected void onPostCreate(Bundle savedInstanceState) {
     74         super.onPostCreate(savedInstanceState);
     75 
     76         // Sync the toggle state after onRestoreInstanceState has occurred.
     77         mDrawerToggle.syncState();
     78     }
     79 
     80     @Override
     81     public boolean onOptionsItemSelected(MenuItem item) {
     82         /*
     83          * The action bar home/up action should open or close the drawer.
     84          * mDrawerToggle will take care of this.
     85          */
     86         if (mDrawerToggle.onOptionsItemSelected(item)) {
     87             return true;
     88         }
     89         return super.onOptionsItemSelected(item);
     90     }
     91 
     92     @Override
     93     public void onConfigurationChanged(Configuration newConfig) {
     94         super.onConfigurationChanged(newConfig);
     95         mDrawerToggle.onConfigurationChanged(newConfig);
     96     }
     97 
     98     /**
     99      * This list item click listener implements very simple view switching by changing
    100      * the primary content text. The drawer is closed when a selection is made.
    101      */
    102     private class DrawerItemClickListener implements ListView.OnItemClickListener {
    103         @Override
    104         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    105             mContent.setText(Shakespeare.DIALOGUE[position]);
    106             mActionBar.setTitle(Shakespeare.TITLES[position]);
    107             mDrawerLayout.closeDrawer(mDrawer);
    108         }
    109     }
    110 
    111     /**
    112      * A drawer listener can be used to respond to drawer events such as becoming
    113      * fully opened or closed. You should always prefer to perform expensive operations
    114      * such as drastic relayout when no animation is currently in progress, either before
    115      * or after the drawer animates.
    116      *
    117      * When using ActionBarDrawerToggle, all DrawerLayout listener methods should be forwarded
    118      * if the ActionBarDrawerToggle is not used as the DrawerLayout listener directly.
    119      */
    120     private class DemoDrawerListener implements DrawerLayout.DrawerListener {
    121         @Override
    122         public void onDrawerOpened(View drawerView) {
    123             mDrawerToggle.onDrawerOpened(drawerView);
    124             mActionBar.onDrawerOpened();
    125         }
    126 
    127         @Override
    128         public void onDrawerClosed(View drawerView) {
    129             mDrawerToggle.onDrawerClosed(drawerView);
    130             mActionBar.onDrawerClosed();
    131         }
    132 
    133         @Override
    134         public void onDrawerSlide(View drawerView, float slideOffset) {
    135             mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
    136         }
    137 
    138         @Override
    139         public void onDrawerStateChanged(int newState) {
    140             mDrawerToggle.onDrawerStateChanged(newState);
    141         }
    142     }
    143 
    144     /**
    145      * Create a compatible helper that will manipulate the action bar if available.
    146      */
    147     private ActionBarHelper createActionBarHelper() {
    148         return new ActionBarHelper();
    149     }
    150 
    151     /**
    152      * Action bar helper for use on ICS and newer devices.
    153      */
    154     private class ActionBarHelper {
    155         private final ActionBar mActionBar;
    156         private CharSequence mDrawerTitle;
    157         private CharSequence mTitle;
    158 
    159         ActionBarHelper() {
    160             mActionBar = getSupportActionBar();
    161         }
    162 
    163         public void init() {
    164             mActionBar.setDisplayHomeAsUpEnabled(true);
    165             mActionBar.setDisplayShowHomeEnabled(false);
    166             mTitle = mDrawerTitle = getTitle();
    167         }
    168 
    169         /**
    170          * When the drawer is closed we restore the action bar state reflecting
    171          * the specific contents in view.
    172          */
    173         public void onDrawerClosed() {
    174             mActionBar.setTitle(mTitle);
    175         }
    176 
    177         /**
    178          * When the drawer is open we set the action bar to a generic title.
    179          * The action bar should only contain data relevant at the top level of
    180          * the nav hierarchy represented by the drawer, as the rest of your content
    181          * will be dimmed down and non-interactive.
    182          */
    183         public void onDrawerOpened() {
    184             mActionBar.setTitle(mDrawerTitle);
    185         }
    186 
    187         public void setTitle(CharSequence title) {
    188             mTitle = title;
    189         }
    190     }
    191 }
    192