NestedScrolling API 介绍

NestedScrollingChild

void setNestedScrollingEnabled(boolean enabled);    // 设置是否开启嵌套滑动
boolean isNestedScrollingEnabled();                 // 获得设置开启了嵌套滑动
boolean startNestedScroll(@ScrollAxis int axes);    // 沿给定的轴线开始嵌套滚动
void stopNestedScroll();                            // 停止当前嵌套滚动
boolean hasNestedScrollingParent();                 // 如果有ns parent,返回true
boolean dispatchNestedPreScroll(int dx
    , int dy
    , @Nullable int[] consumed
    , @Nullable int[] offsetInWindow);              // 消费滑动时间前,先让ns parent消费
boolean dispatchNestedScroll(int dxConsumed
    , int dyConsumed
    , int dxUnconsumed
    , int dyUnconsumed
    , @Nullable int[] offsetInWindow);              // ns parent消费ns child剩余滚动后是否还有剩余。return true代表还有剩余
boolean dispatchNestedPreFling(float velocityX
    , float velocityY);                            // 消费fly速度前,先让ns parent消费
boolean dispatchNestedFling(float velocityX
    , float velocityY
    , boolean consumed);                           // ns parent消费ns child消费后的速度之后是否还有剩余。return true代表还有剩余

NestedScrollingParent

boolean onStartNestedScroll(@NonNull View var1
    , @NonNull View var2
    , int var3);                                  // 决定是否接收子View的滚动事件
void onNestedScrollAccepted(@NonNull View var1
    , @NonNull View var2
    , int var3);                                 // 响应子View的滚动
void onStopNestedScroll(@NonNull View var1);     // 滚动结束的回调
void onNestedPreScroll(@NonNull View var1
    , int var2
    , int var3
    , @NonNull int[] var4);                      // ns child滚动前回调
void onNestedScroll(@NonNull View var1
    , int var2
    , int var3
    , int var4
    , int var5);                                // ns child滚动后回调
boolean onNestedPreFling(@NonNull View var1
    , float var2
    , float var3);                             // ns child flying前回调
boolean onNestedFling(@NonNull View var1
    , float var2
    , float var3
    , boolean var4);                           // ns child flying后回调
int getNestedScrollAxes();                     // 返回当前布局嵌套滚动的坐标轴

NestedScrollingParent2

public interface NestedScrollingParent2 extends NestedScrollingParent {
       /**
    * 即将开始嵌套滑动,此时嵌套滑动还没有开始,由子控件的 startNestedScroll 方法调用
    *
    * @param child  嵌套滑动对应的父类的子类(由于嵌套滑动对于的父控件不必定是一级就能找到的,可能挑了两级父控件的父控件,child的辈分>=target)
    * @param target 具体嵌套滑动的那个子类
    * @param axes   嵌套滑动支持的滚动方向
    * @param type   嵌套滑动的类型,有两种ViewCompat.TYPE_NON_TOUCH fling效果,ViewCompat.TYPE_TOUCH 手势滑动
    * @return true 表示此父类开始接受嵌套滑动,只有true时候,才会执行下面的 onNestedScrollAccepted 等操做
    */
   boolean onStartNestedScroll(@NonNull View child, @NonNull View target, @ScrollAxis int axes,
           @NestedScrollType int type);

   /**
    * 当onStartNestedScroll返回为true时,也就是父控件接受嵌套滑动时,该方法才会调用
    *
    * @param child
    * @param target
    * @param axes
    * @param type
    */
   void onNestedScrollAccepted(@NonNull View child, @NonNull View target, @ScrollAxis int axes,
           @NestedScrollType int type);

   /**
    * 在子控件开始滑动以前,会先调用父控件的此方法,由父控件先消耗一部分滑动距离,而且将消耗的距离存在consumed中,传递给子控件
    * 在嵌套滑动的子View未滑动以前
    * ,判断父view是否优先与子view处理(也就是父view能够先消耗,而后给子view消耗)
    *
    * @param target   具体嵌套滑动的那个子类
    * @param dx       水平方向嵌套滑动的子View想要变化的距离
    * @param dy       垂直方向嵌套滑动的子View想要变化的距离 dy<0向下滑动 dy>0 向上滑动
    * @param consumed 这个参数要咱们在实现这个函数的时候指定,回头告诉子View当前父View消耗的距离
    *                 consumed[0] 水平消耗的距离,consumed[1] 垂直消耗的距离 好让子view作出相应的调整
    * @param type     滑动类型,ViewCompat.TYPE_NON_TOUCH fling效果,ViewCompat.TYPE_TOUCH 手势滑动
    */
   void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed,
           @NestedScrollType int type);
           
   /**
    * 在 onNestedPreScroll 中,父控件消耗一部分距离以后,剩余的再次给子控件,
    * 子控件消耗以后,若是还有剩余,则把剩余的再次还给父控件
    *
    * @param target       具体嵌套滑动的那个子类
    * @param dxConsumed   水平方向嵌套滑动的子控件滑动的距离(消耗的距离)
    * @param dyConsumed   垂直方向嵌套滑动的子控件滑动的距离(消耗的距离)
    * @param dxUnconsumed 水平方向嵌套滑动的子控件未滑动的距离(未消耗的距离)
    * @param dyUnconsumed 垂直方向嵌套滑动的子控件未滑动的距离(未消耗的距离)
    * @param type     滑动类型,ViewCompat.TYPE_NON_TOUCH fling效果,ViewCompat.TYPE_TOUCH 手势滑动
    */
   void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
           int dxUnconsumed, int dyUnconsumed, @NestedScrollType int type);

    /**
    * 中止滑动
    *
    * @param target
    * @param type     滑动类型,ViewCompat.TYPE_NON_TOUCH fling效果,ViewCompat.TYPE_TOUCH 手势滑动
    */
 void onStopNestedScroll(@NonNull View target, @NestedScrollType int type);
}

NestedScrollingChild2

public interface NestedScrollingChild2 extends NestedScrollingChild {

   /**
    * 开始滑动前调用,在惯性滑动和触摸滑动前都会进行调用,此方法通常在 onInterceptTouchEvent或者onTouch中,通知父类方法开始滑动
    * 会调用父类方法的 onStartNestedScroll onNestedScrollAccepted 两个方法
    *
    * @param axes 滑动方向
    * @param type 开始滑动的类型 the type of input which cause this scroll event
    * @return 有父视图而且开始滑动,则返回true 实际上就是看parent的 onStartNestedScroll 方法
    */
   boolean startNestedScroll(@ScrollAxis int axes, @NestedScrollType int type);

  /**
    * 子控件中止滑动,例如手指抬起,惯性滑动结束
    *
    * @param type 中止滑动的类型 TYPE_TOUCH,TYPE_NON_TOUCH
    */
   void stopNestedScroll(@NestedScrollType int type);

    /**
    * 判断是否有父View 支持嵌套滑动
    */
   boolean hasNestedScrollingParent(@NestedScrollType int type);

 /**
    * 在dispatchNestedPreScroll 以后进行调用
    * 当滑动的距离父控件消耗后,父控件将剩余的距离再次交个子控件,
    * 子控件再次消耗部分距离后,又继续将剩余的距离分发给父控件,由父控件判断是否消耗剩下的距离。
    * 若是四个消耗的距离都是0,则表示没有神能够消耗的了,会直接返回false,不然会调用父控件的
    * onNestedScroll 方法,父控件继续消耗剩余的距离
    * 会调用父控件的
    *
    * @param dxConsumed     水平方向嵌套滑动的子控件滑动的距离(消耗的距离)    dx<0 向右滑动 dx>0 向左滑动 (保持和 RecycleView 一致)
    * @param dyConsumed     垂直方向嵌套滑动的子控件滑动的距离(消耗的距离)    dy<0 向下滑动 dy>0 向上滑动 (保持和 RecycleView 一致)
    * @param dxUnconsumed   水平方向嵌套滑动的子控件未滑动的距离(未消耗的距离)dx<0 向右滑动 dx>0 向左滑动 (保持和 RecycleView 一致)
    * @param dyUnconsumed   垂直方向嵌套滑动的子控件未滑动的距离(未消耗的距离)dy<0 向下滑动 dy>0 向上滑动 (保持和 RecycleView 一致)
    * @param offsetInWindow 子控件在当前window的偏移量
    * @return 若是返回true, 表示父控件又继续消耗了
    */
   boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
           int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow,
           @NestedScrollType int type);

   /**
    * 子控件在开始滑动前,通知父控件开始滑动,同时由父控件先消耗滑动时间
    * 在子View的onInterceptTouchEvent或者onTouch中,调用该方法通知父View滑动的距离
    * 最终会调用父view的 onNestedPreScroll 方法
    *
    * @param dx             水平方向嵌套滑动的子控件想要变化的距离 dx<0 向右滑动 dx>0 向左滑动 (保持和 RecycleView 一致)
    * @param dy             垂直方向嵌套滑动的子控件想要变化的距离 dy<0 向下滑动 dy>0 向上滑动 (保持和 RecycleView 一致)
    * @param consumed       父控件消耗的距离,父控件消耗完成以后,剩余的才会给子控件,子控件须要使用consumed来进行实际滑动距离的处理
    * @param offsetInWindow 子控件在当前window的偏移量
    * @param type           滑动类型,ViewCompat.TYPE_NON_TOUCH fling效果,ViewCompat.TYPE_TOUCH 手势滑动
    * @return true    表示父控件进行了滑动消耗,须要处理 consumed 的值,false表示父控件不对滑动距离进行消耗,能够不考虑consumed数据的处理,此时consumed中两个数据都应该为0
    */
   boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed,
           @Nullable int[] offsetInWindow, @NestedScrollType int type);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容