Android Design Support Library( 二 )


这里只贴出一部分 , 如果英文不差的话看得懂注释的意思:大致就是说我们这里只提供API 11以上的和FAB的运动交互效果 , 也就是我们上面动图中看到的效果:当出现了一个时候 , FAB会自动向上移动一段距离 , 当消失的时候FAB会回到原来位置 , 那么如何定义一个属于我们自己的 , 先来看看需要用到的知识:
其实细分的话有两种情况:
1、当一个View的变化依赖于另一个View的尺寸、位置等变化的时候 , 我们只需要关注以下两种方法:
* @param parent 第一个参数不用解释吧* @param 你要依赖别的View的那个View* @param dependency 你要依赖的View* @return return 如果找到了你依赖的那个View就返回true* @see #onDependentViewChanged(CoordinatorLayout, android.view.View, android.view.View)*/public boolean layoutDependsOn(CoordinatorLayout parent, V child, View dependency) {return false;}
* @param parent 同上 , 不解释* @param child 同上* @param dependency 同上* @return 如果这个Behavior改变了child的位置或者尺寸大小就返回true*/public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {return false;}
其实FAB里面就是实现了这两种方法来与交互的 , 看一下标准写法:
@Overridepublic boolean layoutDependsOn(CoordinatorLayout parent,FloatingActionButton child, View dependency) {// We're dependent on all SnackbarLayouts (if enabled)return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;}......@Overridepublic boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child,View dependency) {if (dependency instanceof Snackbar.SnackbarLayout) {updateFabTranslationForSnackbar(parent, child, dependency);} else if (dependency instanceof AppBarLayout) {// If we're depending on an AppBarLayout we will show/hide it automatically// if the FAB is anchored to the AppBarLayoutupdateFabVisibility(parent, (AppBarLayout) dependency, child);}return false;}
2、另一种情况是当一个View监听内部滑动的View进行交互时 , 我们需要关注的方法稍微多一点 , 这些方法都写在了t接口里面 , 而且已经对这个接口有了默认实现:
* @param coordinatorLayout the CoordinatorLayout parent of the view this Behavior is*associated with* @param child the child view of the CoordinatorLayout this Behavior is associated with* @param directTargetChild the child view of the CoordinatorLayout that either is or*contains the target of the nested scroll operation* @param target the descendant view of the CoordinatorLayout initiating the nested scroll* @param nestedScrollAxes the axes that this nested scroll applies to. See*{@link ViewCompat#SCROLL_AXIS_HORIZONTAL},*{@link ViewCompat#SCROLL_AXIS_VERTICAL} 滑动时是横轴和纵轴* @return true if the Behavior wishes to accept this nested scroll** @see NestedScrollingParent#onStartNestedScroll(View, View, int)*/public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,V child, View directTargetChild, View target, int nestedScrollAxes) {return false;}
* @param coordinatorLayout the CoordinatorLayout parent of the view this Behavior is*associated with* @param child the child view of the CoordinatorLayout this Behavior is associated with* @param target the descendant view of the CoordinatorLayout performing the nested scroll* @param dx the raw horizontal number of pixels that the user attempted to scroll* @param dy the raw vertical number of pixels that the user attempted to scroll* @param consumed out parameter. consumed[0] should be set to the distance of dx that*was consumed, consumed[1] should be set to the distance of dy that*was consumed** @see NestedScrollingParent#onNestedPreScroll(View, int, int, int[])*/public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target,int dx, int dy, int[] consumed) {// Do nothing}