/** * Interaction behavior plugin for child views of @link CoordinatorLayout. * * <p>A Behavior implements one or more interactions that a user can take on a child view. * These interactions may include drags, swipes, flings, or any other gestures.</p> * * @param <V> The View type that this Behavior operates on */ public static abstract class Behavior<V extends View> ...........
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) return false;
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) return false;
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) final int layoutDirection = ViewCompat.getLayoutDirection(this); final int childCount = mDependencySortedChildren.size(); for (int i = 0; i < childCount; i++) final View child = mDependencySortedChildren.get(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final Behavior behavior = lp.getBehavior();
if (behavior == null || !behavior.onLayoutChild(this, child, layoutDirection)) onLayoutChild(child, layoutDirection);
final int action = MotionEventCompat.getActionMasked(ev);
if (mBehaviorTouchView != null || (cancelSuper = performIntercept(ev, TYPE_ON_TOUCH))) // Safe since performIntercept guarantees that // mBehaviorTouchView != null if it returns true final LayoutParams lp = (LayoutParams) mBehaviorTouchView.getLayoutParams(); final Behavior b = lp.getBehavior(); if (b != null) handled = b.onTouchEvent(this, mBehaviorTouchView, ev);
public static class Behavior extends CoordinatorLayout.Behavior<FloatingActionButton>
........
@Override public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) // Were dependent on all SnackbarLayouts (if enabled) return SNACKBAR_BEHAVIOR_ENABLED && 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) // If were depending on an AppBarLayout we will show/hide it automatically // if the FAB is anchored to the AppBarLayout updateFabVisibility(parent, (AppBarLayout) dependency, child);
@Override public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) // We depend on any AppBarLayouts return dependency instanceof AppBarLayout;
private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior(); if (behavior instanceof Behavior) // Offset the child, pinning it to the bottom the header-dependency, maintaining // any vertical gap, and overlap final Behavior ablBehavior = (Behavior) behavior; final int offset = ablBehavior.getTopBottomOffsetForScrollingSibling(); child.offsetTopAndBottom((dependency.getBottom() - child.getTop()) + ablBehavior.mOffsetDelta + getVerticalLayoutGap() - getOverlapPixelsForOffset(dependency));
final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior(); if (behavior instanceof Behavior) // Offset the child, pinning it to the bottom the header-dependency, maintaining // any vertical gap, and overlap final Behavior ablBehavior = (Behavior) behavior;