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.

674ae12d99324148551d77de28bc4dbe?s=128

Dave Smith

March 17, 2016
Tweet

Transcript

  1. Mastering CoordinatorLayout Behaviors Dave Smith @devunwired +DaveSmithDev

  2. None
  3. AppBarLayout RecyclerView FloatingActionButton SnackbarLayout

  4. @CoordinatorLayout.DefaultBehavior(FooterBarBehavior.class)
 public class FooterBarLayout extends FrameLayout {
 public FooterBarLayout(Context context)

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

    {
 super(context);
 } …
 } Behavior Basics
  6. <android.support.design.widget.CoordinatorLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 
 <com.example.android.coordinatedeffort.widget.FooterBarLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="bottom"

    app:layout_behavior=".behaviors.FooterBarBehavior"/>
 
 </android.support.design.widget.CoordinatorLayout> Behavior Basics
  7. <android.support.design.widget.CoordinatorLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 
 <com.example.android.coordinatedeffort.widget.FooterBarLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="bottom"

    app:layout_behavior=".behaviors.FooterBarBehavior"/>
 
 </android.support.design.widget.CoordinatorLayout> Behavior Basics
  8. 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
  9. Layout Behaviors Scrolling Behaviors

  10. Layout Behaviors View A Behavior View B layoutDependsOn() View B

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

    View A Behavior Size/Position Change onDependentViewChanged() onDependentViewRemoved()
  12. @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
  13. @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
  14. @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
  15. @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
  16. Layout Behaviors

  17. FAB anchored to AppBarLayout Layout Behaviors

  18. AppBarLayout RecyclerView FloatingActionButton SnackbarLayout layoutDependsOn() app:layout_anchor

  19. None
  20. NestedScrollingChild NestedScrollingParent Touch Events Scroll Events Detour: Nested Scrolling

  21. Touch Event Pre-Scroll Scroll Consumed Detour: Nested Scrolling NestedScrollingChild NestedScrollingParent

  22. Touch Event Pre-Scroll Scroll Consumed Detour: Nested Scrolling RecyclerView CoordinatorLayout

  23. Scrolling Behaviors View A Behavior View B NSChild onStartNestedScroll() onNestedPreScroll()

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

    onStartNestedScroll() View A Behavior View B NSChild Touch Fling
  25. AppBarLayout RecyclerView FloatingActionButton SnackbarLayout layoutDependsOn() app:layout_anchor NestedScrolling Observer

  26. Mastering CoordinatorLayout Behaviors @devunwired +DaveSmithDev milehighandroid.com wiresareobsolete.com