CoordinatorLayout.Behavior<View>方法详解

168 阅读5分钟

/**  
* @author: fuhejian  
* @date: 2023/8/18  
*/  
  
/**  
*  
*/  
public class MyBehavior extends CoordinatorLayout.Behavior<View> {  
  
public MyBehavior() {  
super();  
}  
  
public MyBehavior(Context context, AttributeSet attrs) {  
super(context, attrs);  
}  
  
/**  
* 当前behavior被加入  
* @param params the LayoutParams instance that this Behavior has been attached to  
*/  
@Override  
public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams params) {  
super.onAttachedToLayoutParams(params);  
}  
  
/**  
* 当前MyBehavior被移除  
*/  
@Override  
public void onDetachedFromLayoutParams() {  
super.onDetachedFromLayoutParams();  
}  
  
  
/**  
* 是否拦截触摸事件,返回true表示拦截。
* 1. 当event为cancelEvent时,用于初始化和事件流清理(重新开始事件流)  
*  
* 2. 用于在CoordinatorLayout的onInterceptTouchEvent函数中分发拦截请求,此时触摸事件并不一定在child的范围内。  
*  
* 2. 用于判断当前触摸的view,如果在事件流中首次返回true,则mBehaviorTouchView被赋值为当前触摸的view  
*  
* @param parent the parent view currently receiving this touch event  
* @param child the child view associated with this Behavior  
* @param ev the MotionEvent describing the touch event being processed  
* @return  
*/  
@Override  
public boolean onInterceptTouchEvent(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull MotionEvent ev) {  
return super.onInterceptTouchEvent(parent, child, ev);  
}  
  
/**  
*  
* 1. 当event为cancelEvent时,用于初始化和事件流清理(重新开始事件流) actionUp actionCancel事件也使用默认的cancelEvent进行发送  
*  
* 2. 用于判断当前触摸的view,如果在事件流中首次返回true,则mBehaviorTouchView被赋值为当前触摸的view,会后于onInterceptTouchEvent运行,所以如果onInterceptTouchEvent返回true,则在onInterceptTouchEvent期间就赋值了,最好是在onInterceptTouchEvent执行赋值,因为在ontouchevent期间赋值,onTouchEvent赋值期间会被执行两次相同的event  
*  
*  
* @param parent the parent view currently receiving this touch event  
* @param child the child view associated with this Behavior  
* @param ev the MotionEvent describing the touch event being processed  
* @return  
*/  
@Override  
public boolean onTouchEvent(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull MotionEvent ev) {  
return super.onTouchEvent(parent, child, ev);  
}  
  
/**  
*  
* getScrimOpacity 返回值值大于0时有效,  
*  
* Scrim颜色  
*  
*  
*  
*  
* @param parent the parent view of the given child  
* @param child 设置背景为scrim  
* @return  
*/  
@Override  
public int getScrimColor(@NonNull CoordinatorLayout parent, @NonNull View child) {  
return super.getScrimColor(parent, child);  
}  
  
/**  
* Scrim透明度,  
*  
* @param parent the parent view of the given child  
* @param child the child view above the scrim  
* @return  
*/  
@Override  
public float getScrimOpacity(@NonNull CoordinatorLayout parent, @NonNull View child) {  
return super.getScrimOpacity(parent, child);  
}  
  
/**  
* 没啥用  
*  
* @param parent the parent view of the given child  
* @param child the child view to test  
* @return  
*/  
@Override  
public boolean blocksInteractionBelow(@NonNull CoordinatorLayout parent, @NonNull View child) {  
return super.blocksInteractionBelow(parent, child);  
}  
  
/**  
*  
* CoordinatorLayout 会把所有带有behavior的child都调用一次这个函数,child用于判断此behavior是否依赖于child的behavior  
*  
* 返回true表示:onDependentViewChanged和onDependentViewRemoved会被执行  
*  
* @param parent the parent view of the given child  
* @param child 当前behavior是否依赖该view,  
* @param dependency the proposed dependency of child  
* @return  
*/  
@Override  
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {  
return super.layoutDependsOn(parent, child, dependency);  
}  
  
/**  
* child从CoordinatorLayout移除了  
*  
* @param parent the parent view of the given child  
* @param child layoutDependsOn返回true时的child  
* @param dependency the dependent view that changed  
* @return  
*/  
@Override  
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {  
return super.onDependentViewChanged(parent, child, dependency);  
}  
  
/**  
*  
*  
*  
* child 位置发生改变或者child绘制时回调  
*  
* @param parent the parent view of the given child  
* @param child layoutDependsOn返回true时的child  
* @param dependency the dependent view that has been removed  
*/  
@Override  
public void onDependentViewRemoved(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {  
super.onDependentViewRemoved(parent, child, dependency);  
}  
  
@Override  
public boolean onMeasureChild(@NonNull CoordinatorLayout parent, @NonNull View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {  
return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);  
}  
  
@Override  
public boolean onLayoutChild(@NonNull CoordinatorLayout parent, @NonNull View child, int layoutDirection) {  
return super.onLayoutChild(parent, child, layoutDirection);  
}  
  
/**  
* 不依赖于 layoutDependsOn  
* 返回true表示接收嵌套事件,如果返回true表示,CoordinatorLayout所有具有behavior的child后续都会收到嵌套事件  
* @return  
*/  
@Override  
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {  
return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type);  
}  
  
/**  
* 不依赖于 layoutDependsOn  
* 只有onStartNestedScroll返回true的behavior,才可以收到回调  
*  
* @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 that initiated  
* the nested scroll  
* @param type the type of input which cause this scroll event  
*  
*/  
@Override  
public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int type) {  
super.onStopNestedScroll(coordinatorLayout, child, target, type);  
}  
  
/**  
* 不依赖于 layoutDependsOn  
*  
* 只有onStartNestedScroll返回true的behavior,才可以收到回调  
*  
* @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 已经消耗的  
* @param dyConsumed 已经消耗的  
* @param dxUnconsumed 可消耗的  
* @param dyUnconsumed 可消耗的  
* @param type the type of input which cause this scroll event  
* @param consumed 此behavior消耗的  
*  
*/  
@Override  
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, @NonNull int[] consumed) {  
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed);  
}  
  
  
/**  
* 不依赖于 layoutDependsOn  
* 只有onStartNestedScroll返回true的behavior,才可以收到回调  
*  
* {@link #onNestedScroll(CoordinatorLayout, View, View, int, int, int, int, int, int[])} }  
*  
*/  
@Override  
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {  
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);  
}  
  
/**  
*  
* 不依赖于 layoutDependsOn  
* 只有onStartNestedScroll返回true的behavior,才可以收到回调  
*  
* @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  
*/  
@Override  
public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {  
return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);  
}  
  
@Override  
public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, float velocityX, float velocityY) {  
return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);  
}  
  
@NonNull  
@Override  
public WindowInsetsCompat onApplyWindowInsets(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull WindowInsetsCompat insets) {  
return super.onApplyWindowInsets(coordinatorLayout, child, insets);  
}  
  
/**  
*  
* 请求显示child  
* 所有带有behavior的child都会调用  
*  
*  
* @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 rectangle The rectangle which the child wishes to be on the screen  
* in the child's coordinates  
* @param immediate true to forbid animated or delayed scrolling, false otherwise  
* @return  
*/  
@Override  
public boolean onRequestChildRectangleOnScreen(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull Rect rectangle, boolean immediate) {  
return super.onRequestChildRectangleOnScreen(coordinatorLayout, child, rectangle, immediate);  
}  
  
/**  
* 所有带有behavior的child都会调用  
* @param parent the parent CoordinatorLayout  
* @param child child view to restore from  
* @param state The frozen state that had previously been returned by  
* {@link #onSaveInstanceState}.  
*  
*/  
@Override  
public void onRestoreInstanceState(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull Parcelable state) {  
super.onRestoreInstanceState(parent, child, state);  
}  
  
/**  
* 所有带有behavior的child都会调用  
* @param parent the parent CoordinatorLayout  
* @param child child view to restore from  
*  
* @return  
*/  
@Nullable  
@Override  
public Parcelable onSaveInstanceState(@NonNull CoordinatorLayout parent, @NonNull View child) {  
return super.onSaveInstanceState(parent, child);  
}  
  
@Override  
public boolean getInsetDodgeRect(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull Rect rect) {  
return super.getInsetDodgeRect(parent, child, rect);  
}  
  
}