I have a pretty standard layout using the new design libraries:
<AppBarLayout>
<CollapsingToolbarLayout>
<ImageView/>
<Toolbar/>
</CollapsingToolbarLayout>
</AppBarLayout>
<android.support.v4.widget.NestedScrollView/> <!-- content here -->
What I'm trying to do is to completely hide the whole AppBarLayout
programmatically, to temporarily get rid of the Toolbar
and its collapsing feature.
So I'm calling this:
private void disableCollapsing() {
AppBarLayout.LayoutParams p = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams();
p.setScrollFlags(0);
collapsingToolbarLayout.setLayoutParams(p);
}
to disable the collapsing behavior (works well), and finally this:
@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();
}
I make the AppBarLayout translate to the top (works smoothly), and at the end of the animation set is visibility to View.GONE
.
Issue
At the end of the animation, no matter I also set the visibility to GONE, I can't get the space that was previously occupied by the AppBarLayout
. My NestedScrollView remains confined in the lower half of the screen, as if the AppBarLayout was still there (which is not). How can I fix it?
Before hiding:
After hiding (AppBar translated to the top):
As you can see, the top space is empty and unreachable. The scroll view scrolls inside the margins it had before, as if the visibility change was not measured by the CoordinatorLayout
.
I have tried calling
coordinator.requestLayout()
, with no success.I also tried setting the AppBarLayout as an
app:anchor
for my NestedScrollView, but that screws things up - scroll view ends up taking the whole screen even before hiding.I was thinking of a custom
Behavior
to be set on the scroll view when entering this hidden-AppBar mode, but I can't get started on that.
As mentioned above, setting the
Coordinator.LayoutParams#height
fixes the issue.However, I wanted to express how/when this occurs (not necessarily why):
The
CollaspingToolbarLayout
will exhibit this behavior only when itsapp:layout_scrollFlags
property is set toexitUntilCollapsed
and its nestedToolBar
also has definesapp:layout_collapseMode="pin"
. With this combination of flags, theToolbar
will pin itself to the top of the screen, and this is intentional and sometimes desirable.(snipped for brevity)
In the Fragment/Activity after the view is created, in Kotlin + ViewBinding:
For me, I had to capture the height of the
AppBarLayout
before hiding it to restore it to its original height when I wanted to show it.Disclaimer:
ViewBinding
is not necessary to achieve this, nor is using Kotlin, and it's just what I use to acquire theAppBarLayoout
and make this as terse/sugary as possible.