NestedScrolling机制的学习笔记(三)

今天我们来看下NestedScrolling机制的源码。我们分析源码的目的是看看帮助类到底帮助了我们什么,以及父容器和子元素是怎么样配合的。

我们直接点,直接从NestedScrollingChildHelper开始:

构造方法:

public NestedScrollingChildHelper(View view) {
        mView = view;
}

构造方法接收一个View然后赋值给mView,可见mView就是我们的子元素。

startNestedScroll:

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;
}

这个函数会先执行hasNestedScrollingParent(),根据字面意思我们可以大概猜到这个函数是用来判断父容器是否已经存在了,我们看下这个方法内部是啥:

public boolean hasNestedScrollingParent() {
        return mNestedScrollingParent != null;
}

方法内部很简单,就是判断mNestedScrollingParent这个变量是否被赋值了,是的话就返回true,不是就返回fasle。那么mNestedScrollingParent是什么呢,根据名字我们可以猜到应该是实现了NestedScrolling机制的父容器。那么它是什么时候被赋值的呢,我们继续看startNestedScroll函数的代码。

首先最开始的时候,mNestedScrollingParent是空的,那么就会执行第二个if语句,然后isNestedScrollingEnabled()就是判断子元素是不是开启了NestedScrolling机制,如果开启了,就开始遍历子元素的父容器了,寻找实现了NestedScrolling机制的父容器。

我们跟进ViewParentCompat.onStartNestedScroll这个方法:

public static boolean onStartNestedScroll(ViewParent parent, View child, View target,
            int nestedScrollAxes) {
        return IMPL.onStartNestedScroll(parent, child, target, nestedScrollAxes);
}

发现它返回了IMPL的onStartNestedScroll方法。我们继续跟进:

@Override
        public boolean onStartNestedScroll(ViewParent parent, View child, View target,
                int nestedScrollAxes) {
            if (parent instanceof NestedScrollingParent) {
                return ((NestedScrollingParent) parent).onStartNestedScroll(child, target,
                        nestedScrollAxes);
            }
            return false;
}

发现它返回的是NestedScrollingParent的onStartNestedScroll,也就是这个时刻,完成了对父元素的onStartNestedScroll方法的回调。

那也就是说ViewParentCompat这个类就是帮助我们调用父容器的对应方法的,具体的实现方法没细看,用的应该是代理。

我们继续看startNestedScroll的代码,当找到了实现了机制的父容器后,就把这个父容器赋值给mNestedScrollingParent,然后回调onNestedScrollAccepted方法,然后返回true。

那么总结一下,startNestedScroll方法其实就是去找实现了机制的父容器,找到了就返回true,没找到就返回fasle。

stopNestedScroll:

public void stopNestedScroll() {
        if (mNestedScrollingParent != null) {
            ViewParentCompat.onStopNestedScroll(mNestedScrollingParent, mView);
            mNestedScrollingParent = null;
        }
}

stopNestedScroll方法就是调用父容器的方法,然后把mNestedScrollingParent置空。

dispatchNestedScroll:

public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
                                        int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
        if (isNestedScrollingEnabled() && mNestedScrollingParent != null)
        //  首先判断是否开启了机制,以及是否找到了父容器
        {
            if (dxConsumed != 0 || dyConsumed != 0 || dxUnconsumed != 0 || dyUnconsumed != 0) {
                //这里获取父容器的起始位置
                int startX = 0;
                int startY = 0;
                if (offsetInWindow != null) {
                    mView.getLocationInWindow(offsetInWindow);
                    startX = offsetInWindow[0];
                    startY = offsetInWindow[1];
                }

                //调用父容器的方法
                ViewParentCompat.onNestedScroll(mNestedScrollingParent, mView, dxConsumed,
                        dyConsumed, dxUnconsumed, dyUnconsumed);

                if (offsetInWindow != null) {
                    mView.getLocationInWindow(offsetInWindow);
                    //这里用父容器的终止位置减去起始位置,然后得到偏移量在赋值给offsetInWindow
                    offsetInWindow[0] -= startX;
                    offsetInWindow[1] -= startY;
                }
                return true;
            } else if (offsetInWindow != null) {
                // No motion, no dispatch. Keep offsetInWindow up to date.
                offsetInWindow[0] = 0;
                offsetInWindow[1] = 0;
            }
        }
        return false;
    }

这个方法主要做的就是调用父容器的方法,还有计算出偏移量来,我已经写上了注释,应该不难看懂。

dispatchNestedPreScroll:

public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        if (isNestedScrollingEnabled() && mNestedScrollingParent != null) {
            if (dx != 0 || dy != 0) {
                //获得起始位置
                int startX = 0;
                int startY = 0;
                if (offsetInWindow != null) {
                    mView.getLocationInWindow(offsetInWindow);
                    startX = offsetInWindow[0];
                    startY = offsetInWindow[1];
                }

                //初始化consumed
                if (consumed == null) {
                    if (mTempNestedScrollConsumed == null) {
                        mTempNestedScrollConsumed = new int[2];
                    }
                    consumed = mTempNestedScrollConsumed;
                }
                consumed[0] = 0;
                consumed[1] = 0;
                ViewParentCompat.onNestedPreScroll(mNestedScrollingParent, mView, dx, dy, consumed);

                if (offsetInWindow != null) {
                    //求得偏移量
                    mView.getLocationInWindow(offsetInWindow);
                    offsetInWindow[0] -= startX;
                    offsetInWindow[1] -= startY;
                }
                return consumed[0] != 0 || consumed[1] != 0;
            } else if (offsetInWindow != null) {
                offsetInWindow[0] = 0;
                offsetInWindow[1] = 0;
            }
        }
        return false;
    }

这个方法和上面那个差不多,就多了初始化consumed的步骤。

余下四个方法:

public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        if (isNestedScrollingEnabled() && mNestedScrollingParent != null) {
            return ViewParentCompat.onNestedFling(mNestedScrollingParent, mView, velocityX,
                    velocityY, consumed);
        }
        return false;
}

public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        if (isNestedScrollingEnabled() && mNestedScrollingParent != null) {
            return ViewParentCompat.onNestedPreFling(mNestedScrollingParent, mView, velocityX,
                    velocityY);
        }
        return false;
}

public void onDetachedFromWindow() {
        ViewCompat.stopNestedScroll(mView);
}

public void onStopNestedScroll(View child) {
        ViewCompat.stopNestedScroll(mView);
}

剩下的四个方法,就是调用父容器的方法而已。

我们总结一下NestedScrollingChildHelper做的事情,其实就两件事:
1:找到实现机制的父容器
2:调用父容器里对应的方法

我们接下来看下NestedScrollingParentHelper里的方法:

    public NestedScrollingParentHelper(ViewGroup viewGroup) {
        mViewGroup = viewGroup;
    }

    public void onNestedScrollAccepted(View child, View target, int axes) {
        mNestedScrollAxes = axes;
    }

    public int getNestedScrollAxes() {
        return mNestedScrollAxes;
    }

    public void onStopNestedScroll(View target) {
        mNestedScrollAxes = 0;
    }

很少,除去构造方法,剩下三个方法就是对axes的保存,取出和清零。

总结:

我们发现,两个接口之间方法的互相调用,主要是通过NestedScrollingChildHelper来实现的。我们在之后的项目中,可以学习借鉴这种思路:

1:用两个接口来解耦需要进行交互的view
2:提供封装了接口之间交互逻辑的helper类以方便用户使用接口

好了,这篇文章结束了,之后我会写一个更加复杂的实例来学习以下这个机制。

如果有问题,请在评论区留言,才疏学浅,欢迎大家批评指正。

最后的最后:

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

推荐阅读更多精彩内容

  • java笔记第一天 == 和 equals ==比较的比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量...
    jmychou阅读 1,473评论 0 3
  • 我是昨天才知道Android里有NestedScrolling这个东西的。所以我昨天学习了一下,然后今天来总结一下...
    陈添阅读 1,783评论 1 15
  • 1.import static是Java 5增加的功能,就是将Import类中的静态方法,可以作为本类的静态方法来...
    XLsn0w阅读 1,198评论 0 2
  • 夜空中最亮的一颗星辰 一定听过很多的故事 我在美丽的夜空下 望着最暗的星辰 我想说一说我的故事 风中在吹拂着 我在...
    深海里的时光阅读 333评论 2 3
  • 17年国内出了几部良心电视剧,它们都以各自的形式赚足观众的时间、情感。不论是哪一部剧热播,其剧中人物都不免会被观众...
    一飞5阅读 290评论 0 3