我認爲如果您已經手動打開它,那麼跟蹤抽屜內部的切換器會更清潔一些。這是我最終做的:
public class ManualActionBarDrawerToggle extends ActionBarDrawerToggle {
private static final float MENU_POSITION = 0f;
private static final float ARROW_POSITION = 1.0f;
private final int animationLength;
private final DrawerLayout drawerLayout;
private final Activity activity;
private State currentState;
private enum State { UP_ARROW, MENU }
public ManualActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescriptionResource, int closeDrawerContentDescriptionResource) {
super(activity, drawerLayout, openDrawerContentDescriptionResource, closeDrawerContentDescriptionResource);
animationLength = activity.getResources().getInteger(android.R.integer.config_shortAnimTime);
this.drawerLayout = drawerLayout;
this.activity = activity;
currentState = State.MENU;
}
public void animateToBackArrow() {
ValueAnimator anim = ValueAnimator.ofFloat(MENU_POSITION, ARROW_POSITION);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(animationLength);
anim.start();
currentState = State.UP_ARROW;
}
public void animateToMenu() {
ValueAnimator anim = ValueAnimator.ofFloat(ARROW_POSITION, MENU_POSITION);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float slideOffset = (Float) valueAnimator.getAnimatedValue();
onDrawerSlide(drawerLayout, slideOffset);
}
});
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(animationLength);
anim.start();
currentState = State.MENU;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (android.R.id.home):
if (currentState == State.UP_ARROW) {
activity.onBackPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}
}
確實,正如我所說的,我已經使用過它,但是它不像股票圖標那麼好看:)。 – MathieuMaree 2014-11-04 23:00:54
然後,爲什麼不切換到v7的ActionBarDrawerToggle? – 2014-11-04 23:02:46
嗯,我沒有...?我想要的不是打開/關閉抽屜時的切換動畫;這是相同的動畫,但切換片段時。我希望一旦添加了片段,切換就像家庭/後退箭頭一樣操作 – MathieuMaree 2014-11-04 23:06:11