NestScrollView原理解析

简介

NestedScrolling,包含在android.support.v4包中,由 22.10 版本开始引入,支持 5.0 及 5.0 以上的系统。

NestedScrolling,简称嵌套滑动,可主要分为NestedScrollingParen和NestedScrollingChild两部分,使用它可以实现一些非常绚丽的效果。

Google 帮我们封装好了一些相应的空间,比如 RecyclerView 实现了 NestedScrollingChild 接口,CoordinatorLayout 实现了 NestedScrollingParent 接口,NestedScrollingViewSwipeRefreshLayout 实现了 NestedScrollingChild,NestedScrollingParent 接口等。

那么相比较于传统的事件分发机制,NetstedScroll 机制有什么特点呢?

在传统的事件分发机制 中,一旦某个 View 或者 ViewGroup 消费了事件,就很难将事件交给父 View 进行共同处理。而 NestedScrolling 机制很好地帮助我们解决了这一问题。我们只需要按照规范实现相应的接口即可,子 View 实现 NestedScrollingChild,父 View 实现 NestedScrollingParent ,通过 NestedScrollingChildHelper 或者 NestedScrollingParentHelper 完成交互。

NestedScrolling机制的原理

NestedScrolling 整体主要包含四个类:

  • NestedScrollingParent

在嵌套滑动中,如果父View 想实现 嵌套滑动,要实现这个 NestedScrollingParent 借口,与 NestedScrollingChild 大概有一一对应的关系。

  • NestedScrollingChild

在嵌套滑动中,如果scrolling child 想实现嵌套滑动,必须实现这个借口

  • NestedScrollingChildHelper

实现 Child 和 Parent 交互的逻辑

  • NestedScrollingParentHelper

实现 Child 和 Parent 交互的逻辑

它的处理流程大致如下:

img

  1. scrolling child 在滑动之前,会通过 NestedScrollingChildHelper 查找是否有响应的 scrolling parent,如果有的话,会先询问scrolling parent 是否需要先于scrolling child 滑动,如果需要的话,scrolling parent 进行相应的滑动,并消费一定的距离;
  2. 接着scrolling child 进行相应的滑动,并消耗一定的距离值 dx,dy;
  3. scrolling child 滑动完之后,询问scrolling parent 是否还需要继续进行滑动,需要的话,进行相应的处理;
  4. 滑动结束之后,Scrolling child 会停止滑动,并通过 NestedScrollingChildHelper 通知相应的 Scrolling Parent 停止滑动。

NestedScrollingChild 相关方法

目前已实现改接口的类包括: HorizontalGridView, NestedScrollView, RecyclerView, SwipeRefreshLayout, VerticalGridView

  • boolean startNestedScroll(int axes)

在开始滑动的时候会调用这个方法,axes 代表滑动的方向:ViewCompat.SCROLL_AXIS_HORIZONTAL 代表水平滑动,ViewCompat.SCROLL_AXIS_VERTICAL 代表垂直滑动。返回值是布尔类型的,根据返回值,我们可以判断是否找到支持嵌套滑动的父View ,返回 true,表示在scrolling parent (需要注意的是这里不一定是直接scrolling parent ,间接scrolling parent 也可会返回 true) 中找到支持嵌套滑动的。反之,则找不到。

  • boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow)

在scrolling child 滑动之前,提供机会让scrolling parent 先于scrolling child滑动。

dx,dy 是输入参数,表示scrolling child 传递给 scrolling parent 水平方向,垂直方向上的偏移量,consumed 是输出参数,consumed[0] 表示父 View 在水平方向上消费的值,,consumed[1 表示父 View 在垂直方向上消费的值。

返回值也是布尔类型的,根据这个值 ,我们可以判断scrolling parent 是都消费了相应距离 。

  • boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow)

在scrolling child 滑动之后,调用这个方法,提供机会给scrolling parent 滑动,dxConsumed,dyConsumed 是输入参数,表示scrolling child 在水平方向,垂直方向消耗的值,dxUnconsumed,dyUnconsumed 也是输入参数,表示scrolling child 在水平方向,垂直方向未消耗的值。

  • boolean dispatchNestedPreFling(float velocityX, float velocityY, boolean consumed)

调用这个方法,在scrolling child 处理 fling 动作之前,提供机会scrolling parent 先于scrolling child 处理 fling 动作。

三个参数都是输入参数,velocityX 表示水平方向的速度,velocityY 表示垂直方向感的速度,consumed 表示scrolling child 是否消费 fling 动作 。返回值也是布尔类型的,表示scrolling parent 是否有消费了fling 动作或者对 fling 动作做出相应的 处理。true 表示有,false 表示没有。

  • boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed)

在 Scrolling child 处理 fling 动作之后,提供机会给 Scrolling Parent 处理 fling 动作。各个参数的意义这里就不再意义阐述了,跟 dispatchNestedFling 参数的意义是一样的。

  • void stopNestedScroll

当滑动取消或停止的时候,会调用这个方法。例如在 RecyclerView 中,当 ACTION_UP 或者 ACTION_CANCEL 或者 item 消费了 Touch 事件的时候,会调用这个方法。

NestedScrollingParent主要方法

目前已实现改接口的类包括: CoordinatorLayout, NestedScrollView, SwipeRefreshLayout。它通常是配合 NestedScrollingChild 进行嵌套滑动的。

  • boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)

在 Scrolling Child 开始滑动的时候会调用这个方法

当 Scrolling Child 调用 onStartNestedScroll 方法的时候,通过 NestedScrollingChildHelper 会回调 Scrolling parent 的 onStartNestedScroll 方法,如果返回 true, Scrolling parent 的 onNestedScrollAccepted(View child, View target, int nestedScrollAxes) 方法会被回调。

target 表示发起滑动事件的 View,Child 是 ViewParent 的直接子View,包含 target,nestedScrollAxes 表示滑动方向。

  • void onNestedScrollAccepted(View child, View target, int nestedScrollAxes)

如果 Scrolling Parent 的onStartNestedScroll 返回 true, Scrolling parent 的 onNestedScrollAccepted(View child, View target, int nestedScrollAxes) 方法会被回调。

  • boolean onNestedPreScroll(View target, int dx, int dy, int[] consumed)

在 Scrolling Child 进行滑动之前,Scrolling Parent 可以先于Scrolling Child 进行相应的处理

如果 Scrolling Child 调用 dispatchNestedPreFling(float velocityX, float velocityY) ,通过 NestedScrollingChildHelper 会回调 Scrolling parent 的 onNestedPreScroll 方法

接下来的几个方法,我们不一一介绍了。与 Scrolling Child 方法几乎是一一对应的。

NetsedScrollingChildHelper相关方法

RecyclerView实现了NestedScrollingChild接口,因此我们以RecyclerView为例,详细探究NetsedScrollingChildHelper的具体应用

    public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
   



        ...



        mScrollingChildHelper = new NestedScrollingChildHelper(this);



        setNestedScrollingEnabled(true);



    }



...



 



    @Override



    public void setNestedScrollingEnabled(boolean enabled) {
   



        mScrollingChildHelper.setNestedScrollingEnabled(enabled);



    }



    @Override



    public boolean isNestedScrollingEnabled() {
   



        return mScrollingChildHelper.isNestedScrollingEnabled();



    }



    @Override



    public boolean startNestedScroll(int axes) {
   



        return mScrollingChildHelper.startNestedScroll(axes);



    }



    @Override



    public
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值