Android Design Support Library( 三 )


* @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 velocityX horizontal velocity of the attempted fling* @param velocityY vertical velocity of the attempted fling* @param consumed true if the nested child view consumed the fling* @return true if the Behavior consumed the fling** @see NestedScrollingParent#onNestedFling(View, float, float, boolean)*/public boolean onNestedFling(CoordinatorLayout coordinatorLayout, V child, View target,float velocityX, float velocityY, boolean consumed) {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 dxConsumed horizontal pixels consumed by the target's own scrolling operation* @param dyConsumed vertical pixels consumed by the target's own scrolling operation* @param dxUnconsumed horizontal pixels not consumed by the target's own scrolling*operation, but requested by the user* @param dyUnconsumed vertical pixels not consumed by the target's own scrolling operation,*but requested by the user** @see NestedScrollingParent#onNestedScroll(View, int, int, int, int)*/public void onNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target,int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {// Do nothing}
如果是码农的话上面的英文注释应该不难吧 , 这四个方法的区别如下:
这么看可能太笼统了 , 看一下这一类的实际体现 , 我们自己自定义一个:
public class FadeBehavior extends FloatingActionButton.Behavior {/*** 因为是在XML中使用app:layout_behavior定义静态的这种行为,* 必须实现一个构造函数使布局的效果能够正常工作 。* 否则 Could not inflate Behavior subclass error messages.* @param context* @param attrs*/public FadeBehavior(Context context, AttributeSet attrs) {super();}/*** 处理垂直方向上的滚动事件**@param coordinatorLayout*@param child*@param directTargetChild*@param target*@param nestedScrollAxes*@return*/@Overridepublic boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {// Ensure we react to vertical scrollingreturn nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,nestedScrollAxes);}/*** 检查Y的位置 , 并决定按钮是否动画进入或退出* @param coordinatorLayout* @param child* @param target* @param dxConsumed* @param dyConsumed* @param dxUnconsumed* @param dyUnconsumed*/@Overridepublic void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,dyUnconsumed);if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {// User scrolled down and the FAB is currently visible -> hide the FABchild.hide();} else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {// User scrolled up and the FAB is currently not visible -> show the FABchild.show();}}}
这里继承了FAB的写了一个我们自己的实现 , 注意实现自己的的时候一定要重写两个参数的构造方法 , 因为会从我们在XML中定义的app:属性去找这个 , 了解自定义View的对这个应该不会陌生 , 一般的写法是:
app:layout_behavior=".FadeBehavior "
在查资料的过程中发现很多人把自定义类所在的包名也写进去了 , 其实亲测没必要这样做 , 而且里面也有专门的方法去解析: