Last active
December 17, 2015 10:09
-
-
Save scruffyfox/5592299 to your computer and use it in GitHub Desktop.
Using drawer layout with actionbar sherlock support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.cube.arc.vh; | |
| import android.app.ActionBar; | |
| import android.app.Activity; | |
| import android.content.pm.ActivityInfo; | |
| import android.content.res.Configuration; | |
| import android.os.AsyncTask; | |
| import android.os.Build; | |
| import android.os.Bundle; | |
| import android.support.v4.app.ActionBarDrawerToggle; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.app.FragmentActivity; | |
| import android.support.v4.app.FragmentManager; | |
| import android.support.v4.app.FragmentTransaction; | |
| import android.support.v4.view.GravityCompat; | |
| import android.support.v4.widget.DrawerLayout; | |
| import android.view.View; | |
| import android.widget.FrameLayout; | |
| import com.actionbarsherlock.ActionBarSherlock; | |
| import com.cube.arc.lib.util.Views; | |
| import com.cube.arc.lib.util.Views.InjectView; | |
| public class MainActivity extends FragmentActivity | |
| { | |
| private ActionBarHelper mActionBar; | |
| private ActionBarDrawerToggle mDrawerToggle; | |
| @InjectView(R.id.fragment_holder) public FrameLayout holder; | |
| @InjectView(R.id.drawer_layout) public DrawerLayout drawer; | |
| @Override public void onCreate(Bundle savedInstanceState) | |
| { | |
| super.onCreate(savedInstanceState); | |
| mActionBar = createActionBarHelper(); | |
| mActionBar.init(); | |
| Views.inject(this); | |
| drawer.setDrawerListener(new DrawerListener()); | |
| drawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); | |
| mDrawerToggle = new ActionBarDrawerToggle(this, drawer, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close); | |
| } | |
| @Override protected void onPostCreate(Bundle savedInstanceState) | |
| { | |
| super.onPostCreate(savedInstanceState); | |
| mDrawerToggle.syncState(); | |
| } | |
| @Override public void onConfigurationChanged(Configuration newConfig) | |
| { | |
| super.onConfigurationChanged(newConfig); | |
| mDrawerToggle.onConfigurationChanged(newConfig); | |
| } | |
| private ActionBarHelper createActionBarHelper() | |
| { | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) | |
| { | |
| return new ActionBarHelperICS(); | |
| } | |
| else | |
| { | |
| return new ActionBarHelperGB(this); | |
| } | |
| } | |
| /** | |
| * Stub action bar helper; this does nothing. | |
| */ | |
| private class ActionBarHelper | |
| { | |
| public void init(){} | |
| public void onDrawerClosed(){} | |
| public void onDrawerOpened(){} | |
| public void setTitle(CharSequence title){} | |
| } | |
| private class ActionBarHelperGB extends ActionBarHelper | |
| { | |
| private final ActionBarSherlock mActionBar; | |
| public ActionBarHelperGB(Activity a) | |
| { | |
| mActionBar = ActionBarSherlock.wrap(a); | |
| mActionBar.setContentView(R.layout.activity_main); | |
| } | |
| @Override public void init() | |
| { | |
| mActionBar.setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW); | |
| } | |
| } | |
| private class ActionBarHelperICS extends ActionBarHelper | |
| { | |
| private final ActionBar mActionBar; | |
| private CharSequence mDrawerTitle; | |
| private CharSequence mTitle; | |
| ActionBarHelperICS() | |
| { | |
| mActionBar = getActionBar(); | |
| } | |
| @Override public void init() | |
| { | |
| setContentView(R.layout.activity_main); | |
| mActionBar.setDisplayHomeAsUpEnabled(true); | |
| mActionBar.setHomeButtonEnabled(true); | |
| mTitle = mDrawerTitle = getTitle(); | |
| } | |
| @Override public void onDrawerClosed() | |
| { | |
| super.onDrawerClosed(); | |
| mActionBar.setTitle(mTitle); | |
| } | |
| @Override public void onDrawerOpened() | |
| { | |
| super.onDrawerOpened(); | |
| mActionBar.setTitle(mDrawerTitle); | |
| } | |
| @Override public void setTitle(CharSequence title) | |
| { | |
| mTitle = title; | |
| } | |
| } | |
| private class DrawerListener implements DrawerLayout.DrawerListener | |
| { | |
| @Override public void onDrawerOpened(View drawerView) | |
| { | |
| mDrawerToggle.onDrawerOpened(drawerView); | |
| mActionBar.onDrawerOpened(); | |
| } | |
| @Override public void onDrawerClosed(View drawerView) | |
| { | |
| mDrawerToggle.onDrawerClosed(drawerView); | |
| mActionBar.onDrawerClosed(); | |
| } | |
| @Override public void onDrawerSlide(View drawerView, float slideOffset) | |
| { | |
| mDrawerToggle.onDrawerSlide(drawerView, slideOffset); | |
| } | |
| @Override public void onDrawerStateChanged(int newState) | |
| { | |
| mDrawerToggle.onDrawerStateChanged(newState); | |
| } | |
| } | |
| public void setPage(Class page) | |
| { | |
| FragmentManager manager = getSupportFragmentManager(); | |
| { | |
| FragmentTransaction trans = manager.beginTransaction(); | |
| Fragment f = Fragment.instantiate(this, page.getName(), null); | |
| trans.replace(R.id.fragment_holder, f); | |
| trans.commit(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment