zoukankan      html  css  js  c++  java
  • 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 

  • 相关阅读:
    mysql授权grant
    实际用到的linux小方法
    rpm包与 yum 安装与卸载
    7个优秀的国内外移动端web框架(转)
    (转) 关于成为linux运维工程师需要掌握的技能
    Content-Language:en-US
    hibernate的formula如何使用
    No Hibernate Session bound to thread, and configuration does not allow creat
    spring mvc 配置
    十六进制字符串转化为byte数组
  • 原文地址:https://www.cnblogs.com/songsh/p/6530024.html
Copyright © 2011-2022 走看看