NestScrollView原理解析,做了三年Android

本文深入探讨了NestScrollView在Android中的工作原理,涉及NestedScrollingChild和NestedScrollingParent接口,解释了如何在滚动子视图开始时调用onStartNestedScroll,以及如何在滑动前通过onNestedPreScroll进行处理。同时,文章以RecyclerView为例,展示了NestedScrollingChildHelper在实际应用中的角色和功能。
摘要由CSDN通过智能技术生成

目前已实现改接口的类包括: CoordinatorLayout, NestedScrollView, SwipeRefreshLayout。它通常是配合 NestedScrollingChild 进行嵌套滑动的。

  • boolean onStartNestedScroll(View child, View target, int nestedScrollAxes)

在 Scrolling Child 开始滑动的时候会调用这个方法

当 Scrolling Child 调用 onStartNestedScroll 方法的时候,通过 NestedScrollingChildHelper 会回调 Scrolling parent 的 onStartNestedScroll 方法,如果返回 true, Scrolling parent 的 onNestedScrollAccepted(View child, View target, int nestedScrollAxes) 方法会被回调。

target 表示发起滑动事件的 View,Child 是 ViewParent 的直接子View,包含 target,nestedScrollAxes 表示滑动方向。

  • void onNestedScrollAccepted(View child, View target, int nestedScrollAxes)

如果 Scrolling Parent 的onStartNestedScroll 返回 true, Scrolling parent 的 onNestedScrollAccepted(View child, View target, int nestedScrollAxes) 方法会被回调。

  • boolean onNestedPreScroll(View target, int dx, int dy, int[] consumed)

在 Scrolling Child 进行滑动之前,Scrolling Parent 可以先于Scrolling Child 进行相应的处理

如果 Scrolling Child 调用 dispatchNestedPreFling(float velocityX, float velocityY) ,通过 NestedScrollingChildHelper 会回调 Scrolling parent 的 onNestedPreScroll 方法

接下来的几个方法,我们不一一介绍了。与 Scrolling Child 方法几乎是一一对应的。

[](

)NetsedScrollingChildHelper相关方法

RecyclerView实现了NestedScrollingChild接口,因此我们以RecyclerView为例,详细探究NetsedScrollingChildHelper的具体应用


 public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {



        ...



        mScrollingChildHelper = new NestedScrollingChildHelper(this);



        setNestedScrollingEnabled(true);



    }



...



 







    @Override



    public void setNestedScrollingEnabled(boolean enabled) {



        mScrollingChildHelper.setNestedScrollingEnabled(enabled);



    }



    @Override



    public boolean isNestedScrollingEnabled() {



        return mScrollingChildHelper.isNestedScrollingEnabled();



    }



    @Override



    public boolean startNestedScroll(int axes) {


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值