ActionBarDrawerTogglepublic class ActionBarDrawerToggle extends Object implements DrawerLayout.DrawerListener
Fields Summary |
---|
private static final ActionBarDrawerToggleImpl | IMPL | private static final float | TOGGLE_DRAWABLE_OFFSETFraction of its total width by which to offset the toggle drawable. | private static final int | ID_HOME | private final android.app.Activity | mActivity | private final Delegate | mActivityImpl | private final android.support.v4.widget.DrawerLayout | mDrawerLayout | private boolean | mDrawerIndicatorEnabled | private boolean | mHasCustomUpIndicator | private android.graphics.drawable.Drawable | mHomeAsUpIndicator | private android.graphics.drawable.Drawable | mDrawerImage | private SlideDrawable | mSlider | private final int | mDrawerImageResource | private final int | mOpenDrawerContentDescRes | private final int | mCloseDrawerContentDescRes | private Object | mSetIndicatorInfo |
Constructors Summary |
---|
public ActionBarDrawerToggle(android.app.Activity activity, android.support.v4.widget.DrawerLayout drawerLayout, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes)Construct a new ActionBarDrawerToggle.
The given {@link Activity} will be linked to the specified {@link DrawerLayout}.
The provided drawer indicator drawable will animate slightly off-screen as the drawer
is opened, indicating that in the open state the drawer will move off-screen when pressed
and in the closed state the drawer will move on-screen when pressed.
String resources must be provided to describe the open/close drawer actions for
accessibility services.
this(activity, drawerLayout, !assumeMaterial(activity), drawerImageRes,
openDrawerContentDescRes, closeDrawerContentDescRes);
| public ActionBarDrawerToggle(android.app.Activity activity, android.support.v4.widget.DrawerLayout drawerLayout, boolean animate, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes)Construct a new ActionBarDrawerToggle.
The given {@link Activity} will be linked to the specified {@link DrawerLayout}.
The provided drawer indicator drawable will animate slightly off-screen as the drawer
is opened, indicating that in the open state the drawer will move off-screen when pressed
and in the closed state the drawer will move on-screen when pressed.
String resources must be provided to describe the open/close drawer actions for
accessibility services.
mActivity = activity;
// Allow the Activity to provide an impl
if (activity instanceof DelegateProvider) {
mActivityImpl = ((DelegateProvider) activity).getDrawerToggleDelegate();
} else {
mActivityImpl = null;
}
mDrawerLayout = drawerLayout;
mDrawerImageResource = drawerImageRes;
mOpenDrawerContentDescRes = openDrawerContentDescRes;
mCloseDrawerContentDescRes = closeDrawerContentDescRes;
mHomeAsUpIndicator = getThemeUpIndicator();
mDrawerImage = ContextCompat.getDrawable(activity, drawerImageRes);
mSlider = new SlideDrawable(mDrawerImage);
mSlider.setOffset(animate ? TOGGLE_DRAWABLE_OFFSET : 0);
|
Methods Summary |
---|
private static boolean | assumeMaterial(android.content.Context context)
return context.getApplicationInfo().targetSdkVersion >= 21 &&
(Build.VERSION.SDK_INT >= 21);
| android.graphics.drawable.Drawable | getThemeUpIndicator()
if (mActivityImpl != null) {
return mActivityImpl.getThemeUpIndicator();
}
return IMPL.getThemeUpIndicator(mActivity);
| public boolean | isDrawerIndicatorEnabled()
return mDrawerIndicatorEnabled;
| public void | onConfigurationChanged(android.content.res.Configuration newConfig)This method should always be called by your Activity 's
{@link Activity#onConfigurationChanged(android.content.res.Configuration) onConfigurationChanged}
method.
// Reload drawables that can change with configuration
if (!mHasCustomUpIndicator) {
mHomeAsUpIndicator = getThemeUpIndicator();
}
mDrawerImage = ContextCompat.getDrawable(mActivity, mDrawerImageResource);
syncState();
| public void | onDrawerClosed(android.view.View drawerView){@link DrawerLayout.DrawerListener} callback method. If you do not use your
ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call
through to this method from your own listener object.
mSlider.setPosition(0);
if (mDrawerIndicatorEnabled) {
setActionBarDescription(mOpenDrawerContentDescRes);
}
| public void | onDrawerOpened(android.view.View drawerView){@link DrawerLayout.DrawerListener} callback method. If you do not use your
ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call
through to this method from your own listener object.
mSlider.setPosition(1);
if (mDrawerIndicatorEnabled) {
setActionBarDescription(mCloseDrawerContentDescRes);
}
| public void | onDrawerSlide(android.view.View drawerView, float slideOffset){@link DrawerLayout.DrawerListener} callback method. If you do not use your
ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call
through to this method from your own listener object.
float glyphOffset = mSlider.getPosition();
if (slideOffset > 0.5f) {
glyphOffset = Math.max(glyphOffset, Math.max(0.f, slideOffset - 0.5f) * 2);
} else {
glyphOffset = Math.min(glyphOffset, slideOffset * 2);
}
mSlider.setPosition(glyphOffset);
| public void | onDrawerStateChanged(int newState){@link DrawerLayout.DrawerListener} callback method. If you do not use your
ActionBarDrawerToggle instance directly as your DrawerLayout's listener, you should call
through to this method from your own listener object.
| public boolean | onOptionsItemSelected(android.view.MenuItem item)This method should be called by your Activity 's
{@link Activity#onOptionsItemSelected(android.view.MenuItem) onOptionsItemSelected} method.
If it returns true, your onOptionsItemSelected method should return true and
skip further processing.
if (item != null && item.getItemId() == ID_HOME && mDrawerIndicatorEnabled) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
return true;
}
return false;
| void | setActionBarDescription(int contentDescRes)
if (mActivityImpl != null) {
mActivityImpl.setActionBarDescription(contentDescRes);
return;
}
mSetIndicatorInfo = IMPL
.setActionBarDescription(mSetIndicatorInfo, mActivity, contentDescRes);
| void | setActionBarUpIndicator(android.graphics.drawable.Drawable upDrawable, int contentDescRes)
if (mActivityImpl != null) {
mActivityImpl.setActionBarUpIndicator(upDrawable, contentDescRes);
return;
}
mSetIndicatorInfo = IMPL
.setActionBarUpIndicator(mSetIndicatorInfo, mActivity, upDrawable, contentDescRes);
| public void | setDrawerIndicatorEnabled(boolean enable)Enable or disable the drawer indicator. The indicator defaults to enabled.
When the indicator is disabled, the ActionBar will revert to displaying
the home-as-up indicator provided by the Activity 's theme in the
android.R.attr.homeAsUpIndicator attribute instead of the animated
drawer glyph.
if (enable != mDrawerIndicatorEnabled) {
if (enable) {
setActionBarUpIndicator(mSlider, mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
} else {
setActionBarUpIndicator(mHomeAsUpIndicator, 0);
}
mDrawerIndicatorEnabled = enable;
}
| public void | setHomeAsUpIndicator(android.graphics.drawable.Drawable indicator)Set the up indicator to display when the drawer indicator is not
enabled.
If you pass null to this method, the default drawable from
the theme will be used.
if (indicator == null) {
mHomeAsUpIndicator = getThemeUpIndicator();
mHasCustomUpIndicator = false;
} else {
mHomeAsUpIndicator = indicator;
mHasCustomUpIndicator = true;
}
if (!mDrawerIndicatorEnabled) {
setActionBarUpIndicator(mHomeAsUpIndicator, 0);
}
| public void | setHomeAsUpIndicator(int resId)Set the up indicator to display when the drawer indicator is not
enabled.
If you pass 0 to this method, the default drawable from the theme will
be used.
Drawable indicator = null;
if (resId != 0) {
indicator = ContextCompat.getDrawable(mActivity, resId);
}
setHomeAsUpIndicator(indicator);
| public void | syncState()Synchronize the state of the drawer indicator/affordance with the linked DrawerLayout.
This should be called from your Activity 's
{@link Activity#onPostCreate(android.os.Bundle) onPostCreate} method to synchronize after
the DrawerLayout's instance state has been restored, and any other time when the state
may have diverged in such a way that the ActionBarDrawerToggle was not notified.
(For example, if you stop forwarding appropriate drawer events for a period of time.)
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mSlider.setPosition(1);
} else {
mSlider.setPosition(0);
}
if (mDrawerIndicatorEnabled) {
setActionBarUpIndicator(mSlider, mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
}
|
|