Android 中 Behavior, NestedScrollingParent, NestedScrollingChild 关系

NestedScroll 相关接口:NestedScrollingParent, NestScrollingChild

顶层布局需要用CoordinatorLayout, Behavior 是CoordinatorLayout 内部类

public class CoordinatorLayout extends ViewGroup implements NestedScrollingParent2 {

public static abstract class Behavior<V extends View> {
  
}

}  

NestedScroll 解决了子View 滑动时,父View需要滑动的情况, 子View 把需要滑动的距离传递给父View, 父View 可以消耗一部分距离,然后通知子View

子View滑动回调父View的接口:

   子View父View
startNestedScroll   --开始滑动onStartNestedScroll,onNestedScrollAccepted
dispatchNestedPreScroll  --预滑动onNestedPreScroll
dispatchNestedScroll   --滑动onNestedScroll
stopNestedScroll   --停止滑动onStopNestedScroll

Behavior 是子View 与父View 之间协调者的角色, 父View的 事件它都有实现,在CoordinatorLayout 的方法实现中,都委托Behavior 进行处理。

@Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
            int dxUnconsumed, int dyUnconsumed) {

     // 遍历所有子View for (int i = 0; i < childCount; i++) { final View view = getChildAt(i); if (view.getVisibility() == GONE) { // If the child is GONE, skip... continue; } final LayoutParams lp = (LayoutParams) view.getLayoutParams(); if (!lp.isNestedScrollAccepted(type)) { continue; } final Behavior viewBehavior = lp.getBehavior(); if (viewBehavior != null) {

        //委托给behavior进行处理 viewBehavior.onNestedScroll(this, view, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type); accepted = true; } } if (accepted) { onChildViewsChanged(EVENT_NESTED_SCROLL); } }

  

另外还有两个类:

NestedScrollingChildHelper辅助类
NestedScrollingParentHelper 辅助类

它们是为了适配Android 5.0 之前的版本

前面介绍了NestedScrollingParent, NestedScrollingChild, Behavior 三者之间的关系,下面看下嵌套滑动的常见实现:

1。 Toolbar+ 图片,图片滑动上去时,Toolbar显示

     CoordinatorLayout + ToolbarLayout + CollapsingToolbarLayout + Toolbar 

2.   Toolbar + TabLayout + ViewPager 

 

转载于:https://www.cnblogs.com/songsh/p/6530024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值