【译】Nested Scrolling With CoordinatorLayout On Android

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

软广Github Blog

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

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

源代码

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

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实例就行了。
@Override
public boolean onTouchEvent(MotionEvent event){
 return mDetector.onTouchEvent(event);
}
  • 然后在onDown方法里,我们需要调用startNestedScroll方法并将垂直滚动标记作为参数传进去。
@Override
public boolean onDown(MotionEvent e) {
 startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
 return true;
}
  • 然后是在onScroll方法中调用dispatchNestedPreScroll方法并传入计算所得的Y轴滚动距离;然后继续调用dispatchNestedScroll方法,由于我们的view压根不会滚动所以参数都传0就可以了。
@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方法。
@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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容