博客
关于我
Android 嵌套滑动机制(NestedScrolling)
阅读量:114 次
发布时间:2019-02-27

本文共 1054 字,大约阅读时间需要 3 分钟。

Android在Lollipop版本之后引入了NestedScrolling机制,旨在优化用户体验。该机制通过解决Touch事件分发中的漏洞,确保父View和子View能够协同处理滚动事件。以下是详细解释:

NestedScrolling的作用

NestedScrolling机制允许子View在处理滚动事件时,通知父View进行协同处理。这样,当子View滚动时,父View能够根据情况决定是否处理滚动事件,而不会阻止父View在后续Touch事件中继续处理。

NestedScrollingChild和NestedScrollingParent

  • NestedScrollingChild:由子View实现,负责启动嵌套滚动并与父View交互。
  • NestedScrollingParent:由父View(如CoordinatorLayout)实现,处理嵌套滚动的逻辑。

NestedScrollingChildHelper

该helper类负责与父View的交互,确保Touch事件能够正确传递。它通过调用ViewParentCompat类中的方法,与不同API版本的View交互。

startNestedScroll方法

启动嵌套滚动,查找能够处理的父View,并调用相应的回调方法。该方法返回true,表明嵌套滚动已启动。

dispatchNestedPreScroll和dispatchNestedScroll

  • dispatchNestedPreScroll:在滑动事件前,通知父View滑动距离和偏移量。
  • dispatchNestedScroll:在滑动事件后,汇报滚动情况和未消费的距离。

调用流程

  • startNestedScroll:子View启动嵌套滚动,父View接受并记录。
  • dispatchNestedPreScroll:子View在滑动前调用,父View处理并返回偏移量。
  • dispatchNestedScroll:子View在滑动后调用,父View处理并返回结果。
  • stopNestedScroll:结束嵌套滚动,父View进行最后处理。
  • 代码实现

    子View在处理Touch事件时,调用这些方法,确保父View能够正确处理滚动事件。通过记录手指位置和偏移量,子View能够正确修正滚动量,避免重复处理。

    总结

    NestedScrolling机制通过协同处理Touch事件,解决了之前的漏洞。通过定义接口和helper类,确保子View和父View能够高效协作,提供流畅的用户体验。

    转载地址:http://bxgf.baihongyu.com/

    你可能感兴趣的文章
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>