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

Mastering CoordinatorLayout Behaviors

Mastering CoordinatorLayout Behaviors

Overview of how to implement custom CoordinatorLayout behaviors, and a breakdown of the API surface area.

Avatar for Dave Smith

Dave Smith

March 17, 2016
Tweet

More Decks by Dave Smith

Other Decks in Programming

Transcript

  1. 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 Behavior Basics
  2. Layout Behaviors View A Behavior View B layoutDependsOn() View B

    View A Behavior onMeasure() onLayout() onMeasureChild() onLayoutChild()
  3. Layout Behaviors View A Behavior View B layoutDependsOn() View B

    View A Behavior Size/Position Change onDependentViewChanged() onDependentViewRemoved()
  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;
 } Layout Behaviors
  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;
 } Layout Behaviors
  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;
 } Layout Behaviors
  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? Layout Behaviors
  8. Scrolling Behaviors View A Behavior View B NSChild onStartNestedScroll() onNestedPreScroll()

    onNestedScroll() View A Behavior View B NSChild Touch Scroll
  9. Scrolling Behaviors View A Behavior View B NSChild onNestedPreFling() onNestedFling()

    onStartNestedScroll() View A Behavior View B NSChild Touch Fling