一、关键词Behavior
先来介绍一下Behavior,我们在开发过程中可能会遇到这样一个小需求,就是在Button点击滑动的过程中,还额外的需要一个TextView一起跟着滑动,倒是手指点的是Button上面的,那么怎么去控制TextView的联动效果呢?这里就用到了CoordinatorLayout +Behavior。
二、CoordinatorLayout如何获取到子控件的Behavior?
Behavior作为一个中间者,目的是协调子控件之间的触摸事件。
CoordinatorLayout有两个主要的用法:1.作为顶层应用的装饰或者chrome的布局;2.作为一个能响应特定的一个或多个子视图交互的容器。
三、CoordinatorLayout + NestedScrollingParent 完成补全事件分发所不能完成的事情
总所周知,Android事件纷发机制主要是有三个函数:dispatchTouchEvent/onInterceptTouchEvent和onTouchEvent;
这种纷发机制本身有个漏洞:如果子View获得处理Touch事件的时候,父View就再也没有机会去处理touch事件能力,也就是当前的触摸事件跟父容器没有半点关系了。
但是NestedScrolling机制可以解决这一漏洞:
内部View在滚动的时候,首先将dx,dy交给NestedScrollingParent,NestedScrollingParent可对其进行部分消耗,剩余的部分还给内部子View。
NestedScrolling机制主要和NestedScrollingParent有关,来看NestedScrollingParent的源码:
public interface NestedScrollingParent {
/**
* 当子View调用NestedScrollingChild的方法startNestedScroll时,会调用该方法,一定要按照
*自己的需求返回true,该方法决定了当前控件是否能接收到其内部View滑动的参数。
* @param child Direct child of this ViewParent containing target
* @param target View that initiated the nested scroll
* @param axes Flags consisting of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL},
* {@link ViewCompat#SCROLL_AXIS_VERTICAL} or both
* @return true if this ViewParent accepts the nested scroll operation
*/
boolean onStartNestedScroll(@NonNull View child, @NonNull View target, @ScrollAxis int axes);
/**
* React to the successful claiming of a nested scroll operation.
*
* This method will be called after
* {@link #onStartNestedScroll(View, View, int) onStartNestedScroll} returns true. It offers
* an opportunity for the view and its superclasses to perform initial configuration
* for the nested scroll. Implementations of this method should always call their superclass's
* implementation of this method if one is present.
*
* @param child Direct child of this ViewParent containing target
* @param target View that initiated the nested scroll
* @param axes Flags consisting of {@link ViewCompat#SCROLL_AXIS_HORIZONTAL},
* {@link ViewCompat#SCROLL_AXIS_VERTICAL} or both
* @see #onStartNestedScroll(View, View, int)
* @see #onStopNestedScroll(View)
*/
void onNestedScrollAccepted(@NonNull View child, @NonNull View target, @ScrollAxis int axes);
/**
* React to a nested scroll operation ending.
*
* Perform cleanup after a nested scrolling operation.
* This method will be called when a nested scroll stops, for example when a nested touch
* scroll ends with a {@link MotionEvent#ACTION_UP} or {@link MotionEvent#ACTION_CANCEL} event.
* Implementations of this method should always call their superclass's implementation of this
* method if one is present.
*
* @param target View that initiated the nested scroll
*/
void onStopNestedScroll(@NonNull View target);
/**
*
* React to a nested scroll in progress.
* 当子View调用dispatchNestedPreScroll方法时,会调用该方法,该方法会传入内部View移
* 动的dx,dy;如果你需要消耗一定的dx,dy就通过最后一个参数consumed进行指定。
* 例如我需要消耗一半的dy,就可以写consumed[1] = dy/2
* @param target The descendent view controlling the nested scroll
* @param dxConsumed Horizontal scroll distance in pixels already consumed by target
* @param dyConsumed Vertical scroll distance in pixels already consumed by target
* @param dxUnconsumed Horizontal scroll distance in pixels not consumed by target
* @param dyUnconsumed Vertical scroll distance in pixels not consumed by target
*/
void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed);
/**
* React to a nested scroll in progress before the target view consumes a portion of the scroll.
*
* When working with nested scrolling often the parent view may want an opportunity
* to consume the scroll before the nested scrolling child does. An example of this is a
* drawer that contains a scrollable list. The user will want to be able to scroll the list
* fully into view before the list itself begins scrolling.
*
* onNestedPreScroll
is called when a nested scrolling child invokes
* {@link View#dispatchNestedPreScroll(int, int, int[], int[])}. The implementation should
* report how any pixels of the scroll reported by dx, dy were consumed in the
* consumed
array. Index 0 corresponds to dx and index 1 corresponds to dy.
* This parameter will never be null. Initial values for consumed[0] and consumed[1]
* will always be 0.
*
* @param target View that initiated the nested scroll
* @param dx Horizontal scroll distance in pixels
* @param dy Vertical scroll distance in pixels
* @param consumed Output. The horizontal and vertical scroll distance consumed by this parent
*/
void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed);
/**
* Request a fling from a nested scroll.
*
* This method signifies that a nested scrolling child has detected suitable conditions
* for a fling. Generally this means that a touch scroll has ended with a
* {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds
* the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity}
* along a scrollable axis.
*
* If a nested scrolling child view would normally fling but it is at the edge of
* its own content, it can use this method to delegate the fling to its nested scrolling
* parent instead. The parent may optionally consume the fling or observe a child fling.
*
* @param target View that initiated the nested scroll
* @param velocityX Horizontal velocity in pixels per second
* @param velocityY Vertical velocity in pixels per second
* @param consumed true if the child consumed the fling, false otherwise
* @return true if this parent consumed or otherwise reacted to the fling
*/
boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, boolean consumed);
/**
* React to a nested fling before the target view consumes it.
*
* This method siginfies that a nested scrolling child has detected a fling with the given
* velocity along each axis. Generally this means that a touch scroll has ended with a
* {@link VelocityTracker velocity} in the direction of scrolling that meets or exceeds
* the {@link ViewConfiguration#getScaledMinimumFlingVelocity() minimum fling velocity}
* along a scrollable axis.
*
* If a nested scrolling parent is consuming motion as part of a
* {@link #onNestedPreScroll(View, int, int, int[]) pre-scroll}, it may be appropriate for
* it to also consume the pre-fling to complete that same motion. By returning
* true
from this method, the parent indicates that the child should not
* fling its own internal content as well.
*
* @param target View that initiated the nested scroll
* @param velocityX Horizontal velocity in pixels per second
* @param velocityY Vertical velocity in pixels per second
* @return true if this parent consumed the fling ahead of the target view
*/
boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY);
/**
* Return the current axes of nested scrolling for this NestedScrollingParent.
*
* A NestedScrollingParent returning something other than {@link ViewCompat#SCROLL_AXIS_NONE}
* is currently acting as a nested scrolling parent for one or more descendant views in
* the hierarchy.
*
* @return Flags indicating the current axes of nested scrolling
* @see ViewCompat#SCROLL_AXIS_HORIZONTAL
* @see ViewCompat#SCROLL_AXIS_VERTICAL
* @see ViewCompat#SCROLL_AXIS_NONE
*/
@ScrollAxis
int getNestedScrollAxes();
}