前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >嵌套滑动机制探究

嵌套滑动机制探究

作者头像
提莫队长
发布2018-05-18 15:12:14
7560
发布2018-05-18 15:12:14
举报
文章被收录于专栏:刘晓杰

https://www.cnblogs.com/wjtaigwh/p/6398562.html https://segmentfault.com/a/1190000002873657

按照上面的例子打了log

代码语言:javascript
复制
滑动开始
E/child: startNestedScroll
E/parent: onStartNestedScroll----------------------决定是否要配合其进行嵌套滚动
E/parent: onNestedScrollAccepted--------------------做一些准备动作
E/child: dispatchNestedPreScroll
E/parent: onNestedPreScroll
循环
E/child: startNestedScroll
E/child: dispatchNestedPreScroll
E/parent: onNestedPreScroll

核心还是child: startNestedScroll—child: dispatchNestedPreScroll—parent: onNestedPreScroll这三步。看一下具体实现 child: startNestedScroll—–寻找合适的parent来接受嵌套滑动 (parent: onNestedPreScroll—-先于child滚动。可以自己定制)

代码语言:javascript
复制
public boolean startNestedScroll(int axes, int type) {
    if(this.hasNestedScrollingParent(type)) {
        return true;
    } else {
        if(this.isNestedScrollingEnabled()) {
            ViewParent p = this.mView.getParent();

            for(View child = this.mView; p != null; p = p.getParent()) {
                if(ViewParentCompat.onStartNestedScroll(p, child, this.mView, axes, type)) {
                    this.setNestedScrollingParentForType(type, p);
                    ViewParentCompat.onNestedScrollAccepted(p, child, this.mView, axes, type);
                    return true;
                }

                if(p instanceof View) {
                    child = (View)p;
                }
            }
        }

        return false;
    }
}

二、dispatchNestedPreScroll 在子View的onInterceptTouchEvent或者onTouch中(一般在MontionEvent.ACTION_MOVE事件里),调用该方法通知父View滑动的距离。该方法的第三第四个参数返回父view消费掉的scroll长度和子View的窗体偏移量。如果这个scroll没有被消费完,则子view进行处理剩下的一些距离,由于窗体进行了移动,如果你记录了手指最后的位置,需要根据第四个参数offsetInWindow计算偏移量,才能保证下一次的touch事件的计算是正确的。 如果父view接受了它的滚动参数,进行了部分消费,则这个函数返回true,否则为false。 这个函数一般在子view处理scroll前调用。

三、dispatchNestedScroll 向父view汇报滚动情况,包括子view消费的部分和子view没有消费的部分。 如果父view接受了它的滚动参数,进行了部分消费,则这个函数返回true,否则为false。 这个函数一般在子view处理scroll后调用。

代码语言:javascript
复制
public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow, int type) {
    if(this.isNestedScrollingEnabled()) {
        ViewParent parent = this.getNestedScrollingParentForType(type);
        if(parent == null) {
            return false;
        }

        if(dx != 0 || dy != 0) {
            int startX = 0;
            int startY = 0;
            if(offsetInWindow != null) {
                this.mView.getLocationInWindow(offsetInWindow);
                startX = offsetInWindow[0];
                startY = offsetInWindow[1];
            }

            if(consumed == null) {
                if(this.mTempNestedScrollConsumed == null) {
                    this.mTempNestedScrollConsumed = new int[2];
                }

                consumed = this.mTempNestedScrollConsumed;
            }

            consumed[0] = 0;
            consumed[1] = 0;
            ViewParentCompat.onNestedPreScroll(parent, this.mView, dx, dy, consumed, type);
            if(offsetInWindow != null) {
                this.mView.getLocationInWindow(offsetInWindow);
                offsetInWindow[0] -= startX;
                offsetInWindow[1] -= startY;
            }

            return consumed[0] != 0 || consumed[1] != 0;
        }

        if(offsetInWindow != null) {
            offsetInWindow[0] = 0;
            offsetInWindow[1] = 0;
        }
    }

    return false;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年12月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档