Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Your Act Together with CoordinatorLayout

Dave Smith
September 14, 2015

Getting Your Act Together with CoordinatorLayout

Learn the basics of how Android's CoordinatorLayout behaviors are used, and methodologies for creating custom behaviors.

Dave Smith

September 14, 2015
Tweet

More Decks by Dave Smith

Other Decks in Technology

Transcript

  1. Basic Behaviors public class FooterBarBehavior extends CoordinatorLayout.Behavior<FooterBarLayout> {
 
 public

    FooterBarBehavior() {
 //Used when attached to a view class as the default behavior
 }
 
 public FooterBarBehavior(Context context, AttributeSet attrs) {
 super(context, attrs);
 //Used when attached to a view via XML
 } …
 }
  2. Basic Behaviors public class FooterBarBehavior extends CoordinatorLayout.Behavior<FooterBarLayout> {
 
 public

    FooterBarBehavior() {
 //Used when attached to a view class as the default behavior
 }
 
 public FooterBarBehavior(Context context, AttributeSet attrs) {
 super(context, attrs);
 //Used when attached to a view via XML
 } …
 } Type of Attached View
  3. @Override
 public boolean layoutDependsOn(CoordinatorLayout parent,
 FloatingActionButton child, View dependency) {

    
 return dependency instanceof Snackbar.SnackbarLayout;
 }
 
 @Override
 public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
 if (dependency instanceof Snackbar.SnackbarLayout) {
 updateFabTranslationForSnackbar(parent, child, dependency);
 } else if (dependency instanceof AppBarLayout) {
 updateFabVisibility(parent, (AppBarLayout) dependency, child);
 } 
 return false;
 }
  4. @Override
 public boolean layoutDependsOn(CoordinatorLayout parent,
 FloatingActionButton child, View dependency) {

    
 return dependency instanceof Snackbar.SnackbarLayout;
 }
 
 @Override
 public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
 if (dependency instanceof Snackbar.SnackbarLayout) {
 updateFabTranslationForSnackbar(parent, child, dependency);
 } else if (dependency instanceof AppBarLayout) {
 updateFabVisibility(parent, (AppBarLayout) dependency, child);
 } 
 return false;
 }
  5. @Override
 public boolean layoutDependsOn(CoordinatorLayout parent,
 FloatingActionButton child, View dependency) {

    
 return dependency instanceof Snackbar.SnackbarLayout;
 }
 
 @Override
 public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
 if (dependency instanceof Snackbar.SnackbarLayout) {
 updateFabTranslationForSnackbar(parent, child, dependency);
 } else if (dependency instanceof AppBarLayout) {
 updateFabVisibility(parent, (AppBarLayout) dependency, child);
 } 
 return false;
 }
  6. @Override
 public boolean layoutDependsOn(CoordinatorLayout parent,
 FloatingActionButton child, View dependency) {

    
 return dependency instanceof Snackbar.SnackbarLayout;
 }
 
 @Override
 public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
 if (dependency instanceof Snackbar.SnackbarLayout) {
 updateFabTranslationForSnackbar(parent, child, dependency);
 } else if (dependency instanceof AppBarLayout) {
 updateFabVisibility(parent, (AppBarLayout) dependency, child);
 } 
 return false;
 }
  7. @Override
 public boolean layoutDependsOn(CoordinatorLayout parent,
 FloatingActionButton child, View dependency) {

    
 return dependency instanceof Snackbar.SnackbarLayout;
 }
 
 @Override
 public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) { 
 if (dependency instanceof Snackbar.SnackbarLayout) {
 updateFabTranslationForSnackbar(parent, child, dependency);
 } else if (dependency instanceof AppBarLayout) {
 updateFabVisibility(parent, (AppBarLayout) dependency, child);
 } 
 return false;
 } Where did this come from?
  8. Scrolling Behaviors onStartNestedScroll() onNestedPreScroll() onNestedScroll() Behavior consumes scroll distance before

    child. Behavior reacts to unconsumed scroll distance after child. Behavior declares interest in scrolling event.
  9. Scrolling Behaviors onStartNestedScroll() onNestedPreFling() onNestedFling() Behavior consumes fling before child.

    Behavior reacts to unconsumed fling after child. Behavior declares interest in scrolling event.
  10. Resources • Example CoordinatorLayout Behaviors • http://milehighandroid.com/ • Android Design

    Support Library • https://youtu.be/32i7ot0y78U • https://github.com/chrisbanes/cheesesquare