public interface NestedScrollingChild {
   
    public void setNestedScrollingEnabled(boolean enabled);
    
    public boolean isNestedScrollingEnabled();
 
    public boolean startNestedScroll(int axes);
 
    public void stopNestedScroll();
 
    public boolean hasNestedScrollingParent();
 
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);
 
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);
 
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);
 
    public boolean dispatchNestedPreFling(float velocityX, float velocityY);
}
 
public interface NestedScrollingParent {
   
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
 
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
 
    public void onStopNestedScroll(View target);
 
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,  int dxUnconsumed, int dyUnconsumed);
 
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
 
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);
 
    public int getNestedScrollAxes();
}
 

从上边的方法能看出规律,每当NestScrollingChild调用对应的方法 就会在NestScrollingParent中回调对应的方法

这一系列的嵌套滑动操作事件来源于子View的onTouch事件中 ACTION_DOWN、ACTION_MOV、ACTION_UP

例如以RecyclerView作为子View

ACTION_DOWN:

child.startNestedScroll
childHelper.startNestedScroll
parent.onStartNestedScroll
parent.onNestedScrollAccept (此方法需要在parent.onStartNestedScroll返回true的时候才会去执行,可在此方法中获取一些值或者修改一些值

ACTION_MOVE : 

childHelper.dispatchNestedPreScroll(dy) 先将事件交给了parent处理 调用了下面方法
parent.onNestedPreScroll(dy)

consumedY = parent.onNestedPreScroll(dy)
 dy' = dy - consumeY 

最终剩下的位移,就由childRecyclerView内部消费了调用了scrollByInternal方法

recyclerView.scrollByInternal(dy')

最终发现 在scrollByInternal方法执行了child的dispatchNestedScroll

回调到
parent.onNestedScroll(unconsumeY)

还是先由parent消费之后 再由child去消费

 

ACTION_UP

childHelper.dispatchNestedPreFling
parent.onNestedPreFling
childHelper.dispatchNestedFling
parent.onNestedFling
child.stopNestedScroll

childHelper.stopNestedScroll
parent.onStopNestedScroll

 

posted on 2024-06-19 15:40  毕哥  阅读(92)  评论(0)    收藏  举报