Skip to content

Instantly share code, notes, and snippets.

@codlab
Created April 22, 2016 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codlab/7ee12aa197ce3ce0eb9df23a9d9e7336 to your computer and use it in GitHub Desktop.
Save codlab/7ee12aa197ce3ce0eb9df23a9d9e7336 to your computer and use it in GitHub Desktop.
# Activity declaration
public class MainActivity extends AppCompatActivity
implements AppBarLayout.OnOffsetChangedListener
//int the onCreate
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mCollapsingToolbar.getLayoutParams();
mDefaultFlags = params.getScrollFlags();
mSetSnapAfter0 = false; //set snap after event in the offsetchanged
#OnOffsetChangedListener implementation
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
FragmentDescriptor fragments = FragmentDescriptor.from(sBackstackController.getHead());
if (mSetSnapAfter0 && verticalOffset == 0) {
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mCollapsingToolbar.getLayoutParams();
mSetSnapAfter0 = false;
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); // list other flags here by |
mCollapsingToolbar.setLayoutParams(params);
}
}
#The method to update the toolbar behaviour
//called whenever the toolbar behaviour must be changed : fragment transition, etc...
@UiThread
private void updateCollapsing() {
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) mCollapsingToolbar.getLayoutParams();
FragmentDescriptor frag = FragmentDescriptor.from(sBackstackController.getHead());
ToolbarType type = frag.getToolbarType();
mNewState = type;
if (mSetCanExpandCollapse != null && mSetCanExpandCollapse) {
params.setScrollFlags(mDefaultFlags); //set default
} else if ((mSetCanExpandCollapse != null && !mSetCanExpandCollapse)
|| ToolbarType.EXPANDED.equals(type)) { //COLLAPSED FOR NOW
if (!ToolbarType.COLLAPSED.equals(type)) {
mAppBar.setExpanded(true, true); //set co
mSetSnapAfter0 = true;
} //managed right after
} else {
params.setScrollFlags(mDefaultFlags);
}
//force collapse mode since snap and collapse will not work
if (ToolbarType.COLLAPSED.equals(type)) {
mAppBar.setExpanded(false, true);
mAppBar.setActivated(false);
mCoordinatorLayout.setAllowForScroll(false);
mCollapsingToolbar.setActivated(false);
mCollapsingToolbar.setLayoutParams(params);
} else {
mAppBar.setActivated(true);
mCollapsingToolbar.setActivated(true);
mCoordinatorLayout.setAllowForScroll(true);
mCollapsingToolbar.setLayoutParams(params);
}
}
# Describe the Toolbar current mode
public enum ToolbarType {
NONE,
EXPANDABLE,
EXPANDED,
COLLAPSED
}
# The FragmentDescriptor enum implementation
public enum FragmentDescriptor {
....;
FragmentDescriptor(boolean backable, //well add < in the main activity
ToolbarType toolbar_scrollable,
...) {
}
public boolean isBackable() {
return mBackable;
}
public ToolbarType getToolbarType() {
return mToolbarScrollable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment