请放弃我上面的评论。我对此做了一些研究,我上面的建议没有任何意义。
当您拨打setSupportActionBar(Toolbar) 时,会发生以下情况:
public void setSupportActionBar(Toolbar toolbar) {
....
ToolbarActionBar tbab = new ToolbarActionBar(toolbar, ((Activity) mContext).getTitle(),
mAppCompatWindowCallback);
mActionBar = tbab;
....
}
因此,对getSupportActionBar() 的后续调用会返回ToolbarActionBar 的实例。看看这个类是如何实现我们感兴趣的功能的:
setShowHideAnimationEnabled(boolean):正如你所说,这个方法没有区别。
@Override
public void setShowHideAnimationEnabled(boolean enabled) {
// This space for rent; no-op.
}
show(): 仅使操作栏可见 - 不支持动画。
@Override
public void show() {
// TODO: Consider a better transition for this.
// Right now use no automatic transition so that the app can supply one if desired.
mDecorToolbar.setVisibility(View.VISIBLE);
}
hide(): 仅使操作栏可见 - 同样,不支持动画。
@Override
public void hide() {
// TODO: Consider a better transition for this.
// Right now use no automatic transition so that the app can supply one if desired.
mDecorToolbar.setVisibility(View.GONE);
}
show() 和hide() 中的 cmets 提示开发人员应提供动画过渡。或许,是这样的:
protected void hideActionBar(){
final ActionBar ab = getSupportActionBar();
if (ab != null && ab.isShowing()) {
if(mToolbar != null) {
mToolbar.animate().translationY(-112).setDuration(600L)
.withEndAction(new Runnable() {
@Override
public void run() {
ab.hide();
}
}).start();
} else {
ab.hide();
}
}
}
protected void showActionBar(){
ActionBar ab = getSupportActionBar();
if (ab != null && !ab.isShowing()) {
ab.show();
if(mToolbar != null) {
mToolbar.animate().translationY(0).setDuration(600L).start();
}
}
}
mToolbar.animate()..... 部分是从内存中写入的 - 语法可能不正确 :(。您还可以添加 .alpha(0(during hide) or 1(during show)) 以使过渡看起来更好。
实施:
现在应该清楚的是,getSupportActionBar().show() & hide() 并不关心您如何处理您的 Toolbar 以及这些方法调用。此外,Toolbar 将被视为 Activity 中的任何其他 View。牢记这些要点,问题归结为 - 我们如何动画隐藏(以及稍后显示)View。由于我们需要 Activity 内容与隐藏(或显示)Toolbar 一起滑动,我建议以下实现。请注意,这只是一个基本的常规例程。您当然可以对此进行微调,或者提出一个完全不同的(阅读更好)动画过渡:
// holds the original Toolbar height.
// this can also be obtained via (an)other method(s)
int mToolbarHeight, mAnimDuration = 600/* milliseconds */;
ValueAnimator mVaActionBar;
void hideActionBar() {
// initialize `mToolbarHeight`
if (mToolbarHeight == 0) {
mToolbarHeight = mToolbar.getHeight();
}
if (mVaActionBar != null && mVaActionBar.isRunning()) {
// we are already animating a transition - block here
return;
}
// animate `Toolbar's` height to zero.
mVaActionBar = ValueAnimator.ofInt(mToolbarHeight , 0);
mVaActionBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// update LayoutParams
((AppBarLayout.LayoutParams)mToolbar.getLayoutParams()).height
= (Integer)animation.getAnimatedValue();
mToolbar.requestLayout();
}
});
mVaActionBar.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (getSupportActionBar() != null) { // sanity check
getSupportActionBar().hide();
}
}
});
mVaActionBar.setDuration(mAnimDuration);
mVaActionBar.start();
}
void showActionBar() {
if (mVaActionBar != null && mVaActionBar.isRunning()) {
// we are already animating a transition - block here
return;
}
// restore `Toolbar's` height
mVaActionBar = ValueAnimator.ofInt(0 , mToolbarHeight);
mVaActionBar.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// update LayoutParams
((AppBarLayout.LayoutParams)mToolbar.getLayoutParams()).height
= (Integer)animation.getAnimatedValue();
mToolbar.requestLayout();
}
});
mVaActionBar.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
if (getSupportActionBar() != null) { // sanity check
getSupportActionBar().show();
}
}
});
mVaActionBar.setDuration(mAnimDuration);
mVaActionBar.start();
}
在您的评论中,您提到了I do see an animation now but the space still gets reserved for the toolbar until ab.hide() happens。对我来说,这意味着您使用AppBarLayout 来托管Toolbar。如果不是这样,请告诉我,我们会想办法解决的。
最后,对这些方法的调用将基于:
if (getSupportActionBar().isShowing()) {
hideActionBar();
} else {
showActionBar();
}