<AppBarLayout>
<CollapsingToolbarLayout>
<ImageView/>
<Toolbar/>
</CollapsingToolbarLayout>
</AppBarLayout>
<android.support.v4.widget.NestedScrollView/> <!-- content here -->
我想要做的是通过编程完全隐藏整个AppBarLayout
,暂时摆脱Toolbar
及其折叠特性。
所以我调用了这个方法:
private void disableCollapsing() {
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
p.setScrollFlags(0);
collapsingToolbarLayout.setLayoutParams(p);
}
要禁用折叠行为(很有效),最后使用以下代码:
@Override
public void hide() {
final AppBarLayout layout = (AppBarLayout) findViewById(R.id.appbar);
layout.animate().translationY(-layout.getHeight())
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
layout.setVisibility(View.GONE);
}
}).start();
}
我让AppBarLayout向上平移(效果流畅),并在动画集结束时将其可见性设置为View.GONE
。
问题
在动画结束时,无论我是否将可见性设置为GONE,我都无法获得之前由AppBarLayout
占用的空间。我的NestedScrollView仍然局限于屏幕的下半部分,就好像AppBarLayout仍然存在一样(但实际上不是这样)。我该如何解决?
隐藏之前:
隐藏之后(AppBar向上平移):
可以看到,顶部空间为空且无法触及。滚动视图在之前的边距内滚动,就好像CoordinatorLayout
没有测量到可见性的变化。
我已尝试调用
coordinator.requestLayout()
,但没有成功。我还尝试将AppBarLayout设置为我的NestedScrollView的
app:anchor
,但那会弄乱一切——甚至在隐藏之前,滚动视图就会占据整个屏幕。我考虑创建一个自定义
Behavior
,在进入这种隐藏AppBar模式时将其设置为滚动视图,但我无从开始。