How to reset the Toolbar position controlled by the CoordinatorLayout?
First get a AppBarLayout refrence in you MainActivity, then in the pause state of the fragment that is being replaced, use the method below to expand toolbar :
MainActivity.appbar.setExpanded(true,true);
And or to close the toolbar :
MainActivity.appbar.setExpanded(false,true);
The second parameter is used to scroll the toolbar smoothly.
To reset the scroll state, just get the AppBarLayout.Behavior
object
CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator);
AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
and call onNestedPreScroll
method manually:
int[] consumed = new int[2];
behavior.onNestedPreScroll(coordinator, appbar, null, 0, -1000, consumed);
If you would like to reset smoothly with an animation, you can try calling onNestedFling
instead:
behavior.onNestedFling(coordinator, appbar, null, 0, -1000, true);