NestedScrollingParent 实现复杂交互效果

最终效果:

img

1. 接口

NestedScrollingParent 用来配置子视图( NestedScrollingChild )嵌套滚动。

NestedScrollingParent 简称 NP
NestedScrollingChild 简称 NC

  1. NC 产生一个 touch 事件,调用 startNestedScroll,表示开始分享出去 touch 事件, NP 的 onStartNestedScroll 判断是否需要跟 NC 配合,返回 true 表示接受
  2. NC 调用 dispatchNestedPreScroll ,通知 NP 将要进行一个 Touch 事件,如移动 5个像素,NP 的 onNestedPreScroll 中收到通知,知道 NC 要准备移动5个像素,这时候 NP 正好也需要滑动 2 个像素,然后在 onNestedPreScroll 的参数 consumed[] 数组对应的方向(下标为0的x轴,下标为1的y轴)赋值 2,表示消费 2 个像素
  3. NC 拿到 consumed[] 数组,知道 NP 消费了 2 个像素,剩下了 3 个像素,然后自己根据需要再移动 3 个像素的距离,如果这时候 NC 由于某个原因只移动了 2 个像素,那么剩下的 1 个像素距离会调用 dispatchNestedScroll 给 NP,NP 在 onNestedScroll 处理剩下的未消费的 1 像素。最后,NC 调用 stopNestedScroll ,最后 NP 调用 onStopNestedScroll结束。

  4. 同 NestedScroll 类似的还有一套 NestedFling 操作,整体流程类似,NC 快速滑动产生一个 fling 事件,处理流程 NC.dispatchNestedPreFling -> NP.onNestedPreFling -> NC.dispatchNestedFling -> NP.onNestedFling

为什么 NC 和 NP 能相互配合滚动? 主要的源码在 NestedScrollingChildHelperNestedScrollingParentHelper

效果实现

自定 ViewGroup ,实现 NestedScrollingParent 接口,一个 NestedScrollingParentHelper 成员变量作为辅助

布局

<?xml version="1.0" encoding="utf-8"?>
<xyz.hanks.nestedwebview.nestedscroll.NScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView0"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#20f0"
        android:scrollbarSize="3dp"
        android:scrollbars="vertical"/>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#e1e1e1"
            android:orientation="verti
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值