好的,这需要解释一下。简单来说,你需要获取DrawerToogle
的引用来进行操作。如何获取?你需要提供自己的ActionBarDrawerToggle
类的实现。
这里有一个示例(大量复制粘贴自谷歌自己的实现)
package com.example.navigationdrawertest;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.graphics.drawable.DrawerArrowDrawable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
public class ActionBarDrawerToggle implements DrawerLayout.DrawerListener {
public interface DelegateProvider {
@Nullable
Delegate getDrawerToggleDelegate();
}
public interface Delegate {
void setActionBarUpIndicator(Drawable upDrawable, @StringRes int contentDescRes);
void setActionBarDescription(@StringRes int contentDescRes);
Drawable getThemeUpIndicator();
Context getActionBarThemedContext();
boolean isNavigationVisible();
}
private final Delegate mActivityImpl;
private final DrawerLayout mDrawerLayout;
private DrawerToggle mSlider;
private Drawable mHomeAsUpIndicator;
private boolean mDrawerIndicatorEnabled = true;
private boolean mHasCustomUpIndicator;
private final int mOpenDrawerContentDescRes;
private final int mCloseDrawerContentDescRes;
private View.OnClickListener mToolbarNavigationClickListener;
private boolean mWarnedForDisplayHomeAsUp = false;
public ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout,
Toolbar toolbar, @StringRes int openDrawerContentDescRes,
@StringRes int closeDrawerContentDescRes) {
this(activity, toolbar, drawerLayout, null, openDrawerContentDescRes,
closeDrawerContentDescRes);
}
<T extends Drawable & DrawerToggle> ActionBarDrawerToggle(Activity activity, Toolbar toolbar,
DrawerLayout drawerLayout, T slider,
@StringRes int openDrawerContentDescRes,
@StringRes int closeDrawerContentDescRes) {
mActivityImpl = new ToolbarCompatDelegate(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerIndicatorEnabled) {
toggle();
} else if (mToolbarNavigationClickListener != null) {
mToolbarNavigationClickListener.onClick(v);
}
}
});
mDrawerLayout = drawerLayout;
mOpenDrawerContentDescRes = openDrawerContentDescRes;
mCloseDrawerContentDescRes = closeDrawerContentDescRes;
if (slider == null) {
mSlider = new DrawerArrowDrawableToggle(activity,
mActivityImpl.getActionBarThemedContext());
} else {
mSlider = slider;
}
mHomeAsUpIndicator = getThemeUpIndicator();
}
public void syncState() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mSlider.setPosition(1);
} else {
mSlider.setPosition(0);
}
if (mDrawerIndicatorEnabled) {
setActionBarUpIndicator((Drawable) mSlider,
mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
}
}
public void onConfigurationChanged(Configuration newConfig) {
if (!mHasCustomUpIndicator) {
mHomeAsUpIndicator = getThemeUpIndicator();
}
syncState();
}
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home && mDrawerIndicatorEnabled) {
toggle();
return true;
}
return false;
}
private void toggle() {
int drawerLockMode = mDrawerLayout.getDrawerLockMode(GravityCompat.START);
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)
&& (drawerLockMode != DrawerLayout.LOCK_MODE_LOCKED_OPEN)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else if (drawerLockMode != DrawerLayout.LOCK_MODE_LOCKED_CLOSED) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
public void setHomeAsUpIndicator(Drawable indicator) {
if (indicator == null) {
mHomeAsUpIndicator = getThemeUpIndicator();
mHasCustomUpIndicator = false;
} else {
mHomeAsUpIndicator = indicator;
mHasCustomUpIndicator = true;
}
if (!mDrawerIndicatorEnabled) {
setActionBarUpIndicator(mHomeAsUpIndicator, 0);
}
}
public void setHomeAsUpIndicator(int resId) {
Drawable indicator = null;
if (resId != 0) {
indicator = mDrawerLayout.getResources().getDrawable(resId);
}
setHomeAsUpIndicator(indicator);
}
public boolean isDrawerIndicatorEnabled() {
return mDrawerIndicatorEnabled;
}
public void setDrawerIndicatorEnabled(boolean enable) {
if (enable != mDrawerIndicatorEnabled) {
if (enable) {
setActionBarUpIndicator((Drawable) mSlider,
mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
} else {
setActionBarUpIndicator(mHomeAsUpIndicator, 0);
}
mDrawerIndicatorEnabled = enable;
}
}
public DrawerToggle getSlider() {
return mSlider;
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
mSlider.setPosition(Math.min(1f, Math.max(0, slideOffset)));
}
@Override
public void onDrawerOpened(View drawerView) {
mSlider.setPosition(1);
if (mDrawerIndicatorEnabled) {
setActionBarDescription(mCloseDrawerContentDescRes);
}
}
@Override
public void onDrawerClosed(View drawerView) {
mSlider.setPosition(0);
if (mDrawerIndicatorEnabled) {
setActionBarDescription(mOpenDrawerContentDescRes);
}
}
@Override
public void onDrawerStateChanged(int newState) {
}
public View.OnClickListener getToolbarNavigationClickListener() {
return mToolbarNavigationClickListener;
}
public void setToolbarNavigationClickListener(
View.OnClickListener onToolbarNavigationClickListener) {
mToolbarNavigationClickListener = onToolbarNavigationClickListener;
}
void setActionBarUpIndicator(Drawable upDrawable, int contentDescRes) {
if (!mWarnedForDisplayHomeAsUp && !mActivityImpl.isNavigationVisible()) {
Log.w("ActionBarDrawerToggle", "DrawerToggle may not show up because NavigationIcon"
+ " is not visible. You may need to call "
+ "actionbar.setDisplayHomeAsUpEnabled(true);");
mWarnedForDisplayHomeAsUp = true;
}
mActivityImpl.setActionBarUpIndicator(upDrawable, contentDescRes);
}
void setActionBarDescription(int contentDescRes) {
mActivityImpl.setActionBarDescription(contentDescRes);
}
Drawable getThemeUpIndicator() {
return mActivityImpl.getThemeUpIndicator();
}
static class DrawerArrowDrawableToggle extends DrawerArrowDrawable implements DrawerToggle {
private final Activity mActivity;
public DrawerArrowDrawableToggle(Activity activity, Context themedContext) {
super(themedContext);
mActivity = activity;
}
public void setPosition(float position) {
if (position == 1f) {
setVerticalMirror(true);
} else if (position == 0f) {
setVerticalMirror(false);
}
setProgress(position);
}
public float getPosition() {
return getProgress();
}
}
public interface DrawerToggle {
public void setPosition(float position);
public float getPosition();
}
static class ToolbarCompatDelegate implements Delegate {
final Toolbar mToolbar;
final Drawable mDefaultUpIndicator;
final CharSequence mDefaultContentDescription;
ToolbarCompatDelegate(Toolbar toolbar) {
mToolbar = toolbar;
mDefaultUpIndicator = toolbar.getNavigationIcon();
mDefaultContentDescription = toolbar.getNavigationContentDescription();
}
@Override
public void setActionBarUpIndicator(Drawable upDrawable, @StringRes int contentDescRes) {
mToolbar.setNavigationIcon(upDrawable);
setActionBarDescription(contentDescRes);
}
@Override
public void setActionBarDescription(@StringRes int contentDescRes) {
if (contentDescRes == 0) {
mToolbar.setNavigationContentDescription(mDefaultContentDescription);
} else {
mToolbar.setNavigationContentDescription(contentDescRes);
}
}
@Override
public Drawable getThemeUpIndicator() {
return mDefaultUpIndicator;
}
@Override
public Context getActionBarThemedContext() {
return mToolbar.getContext();
}
@Override
public boolean isNavigationVisible() {
return true;
}
}
}
通过调用 actionBarDrawerToggle.getSlider()
,您可以获得对 DrawerToogle
的引用,并通过调用 drawerToogle.setPosition(some_float_between_0_and_1)
来动画化状态。
要查看完整的工作示例,请访问我的 Github 存储库 此处。