NestedScrolling(Android嵌套滑动机制)

终于要在segmentfault写第一篇文章了,好鸡冻,这篇文章原本打算写在简书上的,可是因为页面不能富文本和markdown同时支持,看到Gemini大神的文章中酷炫、赏心悦目的效果后果断放弃简书,看文章原本就会枯燥,若是再没有美观的效果,那岂不是要边看边睡? 互联网给了咱们这么多选择,那我确定选择体验最棒的。感谢segmentfaulthtml

具体效果能够对比一下:
segmentfault效果
简书效果android

重点文字标记在segmentfault上支持code标签,简书上最多只能经过粗体实现。(反正我是没有找到更好的方法)segmentfault

说到Gemini,我也是这两天由于了解NestedScrolling时接触到的,粗略看了一下资料和文章浏览数,赞! 个人大神!数组

好,前番就到这了,开始正题NestedScrollingmarkdown

以前了解NestedScrolling的时候看过一些博客,其中就包括Gemini的segmentfault,当时看的时候由于不仔细不觉得然,最后才发现这篇博客是对NestedScrolling介绍最清楚的,做为惩罚也好膜拜也罢,把原本能够cv过来的博客手动敲一遍,顺便补充一下本身的一些额外理解。ide

再次感谢Gemini函数


Android 在发布 Lillipop 版本后,为了更好的用户体验,Google为Android的滑动机制提供了NestedScrolling机制。oop

NestedScrolling的特性能够体如今哪儿呢?
好比你用了Toolbar,下面一个ScrollView,向上滚动隐藏Toolbar,向下滚动显示Toolbar,这里在逻辑上就是一个NestedScrolling ——由于你在滚动整个Toolbar在内的View的过程当中,又嵌套滚动了里边的ScrollViewthis

如图:
NestedScrolling_sample_gifgoogle

在这以前,咱们知道Android对Touch事件分发是有本身的一套机制。主要是有三个函数:
dispatchTouchEvent, onInterceptTouchEvent, onTouchEvent

这种分发机制有一个漏洞:

若是子view得到处理touch事件机会的时候,父view就再也没有机会处理这次touch事件,直到下一次手指触发。

也就是说,咱们在滑动子view的时候,若是子view对这个滑动事件不须要处理的时候,只能抛弃这个touch事件,而不会传给父view去处理。

但Google新的NestedScrolling机制就很好的解决了这个问题。

NestedScrolling主要有四个类须要关注:

NestedScrollingChild
NestedScrollingChildHelper
NestedScrollingParent
NestedScrollingParentHelper

以上四个类都在support-v4包中提供,Lollipop中部分View默认实现了NestedScrollingChildNestedScrollingParent

v4包中NestedScrollView同时实现了NestedScrollingChild和NestedScrollingParent。

通常实现NestedScrollingChild就能够了,父View用support-design提供的实现了NestedScrollingParentCoordinatorLayout便可。

@Override
    public void setNestedScrollingEnabled(boolean enabled) {
        super.setNestedScrollingEnabled(enabled);
        mChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return mChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return mChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        mChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return mChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }

简单逻辑这样就能够实现嵌套滑动。

以上接口都是业务逻辑中本身调用,NestedScrollingChildHelper是如何实现的呢? 先看一下startNestedScroll方法

/**
     * Start a new nested scroll for this view.
     *
     * <p>This is a delegate method. Call it from your {@link android.view.View View} subclass
     * method/{@link NestedScrollingChild} interface method with the same signature to implement
     * the standard policy.</p>
     *
     * @param axes Supported nested scroll axes.
     *             See {@link NestedScrollingChild#startNestedScroll(int)}.
     * @return true if a cooperating parent view was found and nested scrolling started successfully
     */
    public boolean startNestedScroll(int axes) {
        if (hasNestedScrollingParent()) {
            // Already in progress
            return true;
        }
        if (isNestedScrollingEnabled()) {
            ViewParent p = mView.getParent();
            View child = mView;
            while (p != null) {
                if (ViewParentCompat.onStartNestedScroll(p, child, mView, axes)) {
                    mNestedScrollingParent = p;
                    ViewParentCompat.onNestedScrollAccepted(p, child, mView, axes);
                    return true;
                }
                if (p instanceof View) {
                    child = (View) p;
                }
                p = p.getParent();
            }
        }
        return false;
    }

能够看到这里是帮你实现了与NestedScrollingParent交互的一些方法。
ViewParentCompat是一个和父View交互的兼容类,判断API version,若是在Lollipop上就调用View自带的方法,不然判断若是实现了NestedScrollingParent,则调用实现接口的方法。

子View与父View的交互流程以下:

1、startNestedScroll

首先子View须要开启整个流程(经过屏幕滑动触发touch事件),经过NestedScrollingChildHelper找到并通知实现了NestedScrollingParent的父View中onStartNestedScrollonNestedScrollAccepted方法。

2、dispatchNestedPreScroll

在子View的onIterceptTouchEventonTouch中(通常在MontionEvent.ACTION_MOVE事件里),调用该方法通知父View的滑动距离,该方法的第三第四个参数返回父View消费掉的scroll长度和子View的窗口偏移量,若是这个scroll没有被消费完,则子View处理剩余距离,因为窗口被移动,若是记录了手指最后的位置,须要根据第四个参数offsetInWindow计算偏移量,才能保证下一次touch事件的计算是正确的。

若是父View接受了滚动参数并部分消费,则该函数返回true,不然返回false。
该函数通常在子View处理Scroll前调用。

3、dispatchNestedScroll

向父View汇报滚动状况,包括子View已消费和未消费的值。
若是父View接受了滚动参数,部分消费则函数返回true,不然返回false。
该函数通常在子View处理Scroll后调用。

4、stopNestedScroll

结束整个嵌套滑动流程。

流程中NestedScrollingChildNestedScrollingParent对应以下:

NestedScrollingChildImpl NestedScrollingParentImpl
onStartNestedScroll onStartNestedScroll, onNestedScrollAccepted
dispatchNestedPreScroll onNestedPreScroll
dispatchNestedScroll onNestedScroll
stopNestedScroll onStopNestedScroll

通常是子View发起调用,父View接受回调。

须要关注dispatchNestedPreScroll中的consumed参数:

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

该参数是一个int类型的数组,长度为2,第一个元素是父View消费的x轴方向的滚动距离,第二个元素是父View消费的y轴方向的滚动距离,若是两个值均不为0,则表示父View已消费滚动距离,则须要对子View滚动距离进行修正,正由于有该参数,使得处理滚动事件时思路更加清晰,不会像之前同样被一堆滚动参数搞混。


本身理解的NestedScrolling简要流程图(不包含Fling事件及返回值的逻辑):

NestedScrolling简要流程图


鸣谢:

Gemini大神segmentFault博客

crainzy CSDN博客

相关文章
相关标签/搜索