Slide 1

Slide 1 text

Getting Your Act Together with CoordinatorLayout Dave Smith NewCircle, Inc. @devunwired ● +DaveSmithDev

Slide 2

Slide 2 text

"CoordinatorLayout is a super-powered FrameLayout"

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

AppBarLayout RecyclerView FloatingActionButton SnackbarLayout

Slide 5

Slide 5 text

@CoordinatorLayout.DefaultBehavior(FooterBarBehavior.class)
 public class FooterBarLayout extends FrameLayout {
 public FooterBarLayout(Context context) {
 super(context);
 } …
 }

Slide 6

Slide 6 text

@CoordinatorLayout.DefaultBehavior(FooterBarBehavior.class)
 public class FooterBarLayout extends FrameLayout {
 public FooterBarLayout(Context context) {
 super(context);
 } …
 }

Slide 7

Slide 7 text


 


Slide 8

Slide 8 text


 


Slide 9

Slide 9 text

Basic Behaviors public class FooterBarBehavior extends CoordinatorLayout.Behavior {
 
 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
 } …
 }

Slide 10

Slide 10 text

Basic Behaviors public class FooterBarBehavior extends CoordinatorLayout.Behavior {
 
 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

Slide 11

Slide 11 text

Layout Behaviors layoutDependsOn() onLayoutChild() onDependentViewChanged() onMeasureChild() onDependentViewRemoved()

Slide 12

Slide 12 text

@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;
 }

Slide 13

Slide 13 text

@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;
 }

Slide 14

Slide 14 text

@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;
 }

Slide 15

Slide 15 text

@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;
 }

Slide 16

Slide 16 text

@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?

Slide 17

Slide 17 text

Anchor Views

Slide 18

Slide 18 text

Anchor Views FAB anchored to AppBarLayout

Slide 19

Slide 19 text

AppBarLayout RecyclerView FloatingActionButton SnackbarLayout layoutDependsOn() app:layout_anchor

Slide 20

Slide 20 text

Nested Scrolling RecyclerView CoordinatorLayout Touch Events Scroll Events

Slide 21

Slide 21 text

Nested Scrolling NestedScrollingChild NestedScrollingParent Touch Events Scroll Events

Slide 22

Slide 22 text

Nested Scrolling NestedScrollingChild NestedScrollingParent Touch Event Pre-Scroll Scroll Consumed

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Resources • Example CoordinatorLayout Behaviors • http://milehighandroid.com/ • Android Design Support Library • https://youtu.be/32i7ot0y78U • https://github.com/chrisbanes/cheesesquare