【译】Nested Scrolling With CoordinatorLayout On Android

此篇blog为译文,原文点击这里

在上一篇blog中我们简要介绍了CoordinatorLayout和自定义Behaviour。评论中有些人询问怎样编写一个可以和CoordinatorLayout还有AppBarLayout协同工作的scroll view。也有些读者好奇为什么AppBarLayout可以和RecyclerView一起工作而和ListView却不行。

我们先来看看AppBarLayout随着RecyclerView上下滚动而消失和显示的效果吧。

源代码

查看AppBarLayout的源码可以发现,AppBarLayout定义了default Behaviour@DefaultBehavior(AppBarLayout.Behavior.class)
AppBarLayout.Behavior实现了下列和scroll事件相关的方法:

1
2
3
4
5
void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed)
void onNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)
void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes)
boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes)
void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target)

这就解释了为什么AppBarLayout被放在CoordinatorLayout里面就能够移动自己的位置。那么问题来了,CoordinatorLayout是怎么知道子view发生滚动了的呢。

瞅瞅NestedScrollingChild和NestedScrollingParent吧

查看源码发现RecyclerView实现了NestedScrollingChild接口。让我们看看文档咋说的:

This interface should be implemented by View subclasses that wish to support dispatching nested scrolling operations to a cooperating parent ViewGroup.

翻译一下吧:需要发送nested scrolling操作给父view的子view需要实现该接口。

CoordinatorLayout处理子view发送的nested scrolling操作,所以实现了NestedScrollingParent接口。文档解释如下:

This interface should be implemented by ViewGroup subclasses that wish to support scrolling operations delegated by a nested child view.

翻译一下:需要处理子view委派的scroll操作的viewgroup都应该实现该接口

接下来让我们深入看看NestedScrollingChildNestedScrollingParent之间是如何交流协作的。

当child开始开始滚动的时候,会调用startNestedScroll方法。接着在每一次执行onScroll的时候都会调用dispatchNestedPreScrolldispatchNestedScroll方法。dispatchNestedPreScroll给父view提供了消耗部分或者全部滚动操作的机会,如果该方法返回true则子view必须在执行滚动操作的时候减去父view消耗的部分。而dispatchNestedScroll方法用于向父view报告自身的滚动情况,包括自身消耗的部分和未消耗的部分。父view对这两个值的处理也可能会有所不同:

An implementation may choose to use the consumed portion to match or chase scroll position of multiple child elements, for example. The unconsumed portion may be used to allow continuous dragging of multiple scrolling or draggable elements, such as scrolling a list within a vertical drawer where the drawer begins dragging once the edge of inner scrolling content is reached.

翻译一下:例如在有的viewgroup中会根据子view消耗的部分来对齐或者追踪其他子view的滚动位置。而没有消耗的部分可能会被嵌套scroll或者可拖动view消耗,比如,在一个drawer里面滚动list,当list已经滚动到边缘了用户还在继续滑动操作,此时就能拖动drawer了。

CoordinatorLayout就像是一个代理,接收子view的回调,并传递给其他子view的Behavior对象。
当滑动结束的时候,子view会调用stopNestedScroll方法。

自定义NestedScrollingChild View

我们已经知道原理了,下一步就该实现一个简单的NestedScrollingChild对象来监控scroll事件并委托给CoordinatorLayout对象处理。

  • 让我们从自定义view开始

public class NestedScrollingChildView extends View implements NestedScrollingChild, OnGestureListener

为了让我们的开发更为简单,谷歌引入了NestedScrollingChildHelper

Helper class for implementing nested scrolling child views compatible with Android platform versions earlier than Android 5.0 Lollipop (API 21).

我们需要做的仅仅是创建helper对象并将各个事件委托给helper处理。

在默认情况下nested scroll是disabled状态,所以需要在构造函数中开启该功能

setNestedScrollingEnabled(true);

  • 接着我们需要监测scroll事件,为了方便我们直接使用GestureDetectorCompat实例就行了。

    1
    2
    3
    4
    @Override
    public boolean onTouchEvent(MotionEvent event){
    return mDetector.onTouchEvent(event);
    }
  • 然后在onDown方法里,我们需要调用startNestedScroll方法并将垂直滚动标记作为参数传进去。

    1
    2
    3
    4
    5
    @Override
    public boolean onDown(MotionEvent e) {
    startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
    return true;
    }
  • 然后是在onScroll方法中调用dispatchNestedPreScroll方法并传入计算所得的Y轴滚动距离;然后继续调用dispatchNestedScroll方法,由于我们的view压根不会滚动所以参数都传0就可以了。

    1
    2
    3
    4
    5
    6
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    dispatchNestedPreScroll(0, (int) distanceY, null, null);
    dispatchNestedScroll(0, 0, 0, 0, null);
    return true;
    }
  • 最后一步就是调用stopNestedScroll了。因为GestureDetectorCompat并没有提供合适的回调,所以我们必须自己在onTouchEvent方法中调用stopNestedScroll方法。

    1
    2
    3
    4
    5
    6
    7
    8
    @Override
    public boolean onTouchEvent(MotionEvent event){
    final boolean handled = mDetector.onTouchEvent(event);
    if (!handled && event.getAction() == MotionEvent.ACTION_UP) {
    stopNestedScroll();
    }
    return true;
    }

    为了简单,咱就没有去处理fling的情况了。

    来吧,看看效果吧。

    恩,文中所有的代码都在https://github.com/ggajews/nestedscrollingchildviewdemo