完全搞懂CoordinatorLayout Behavior 你能做些什么

完全搞懂CoordinatorLayout Behavior 你能做些什么
完全搞懂CoordinatorLayout Behavior 系列之API讲解
完全搞懂CoordinatorLayout Behavior之源码学习
完全搞懂CoordinatorLayout Behavior之实战一

国外的一些应用经常可以看到一些比较炫酷的滑动动画交互,例如折叠、 放大缩小、平移、透明度变化等等,最近几年国内也有很多app逐渐使用了这种动画。比如这些。
蚂蚁财富 APP首页
小米浏览器首页效果
58同城首页,之前有个很炫酷的首页动画,现在找不到了
我自己设计的一个首页动画半成品, 剩下一个title部分没有完成
只有完全搞懂CoordinatorLayout Behavior 相关的知识点,包括原理我们才能够对上面的各种动态效果信手拈来。在这之前我说一个我自己的经历,在我自己项目中,我之前第一次看到58同城的那个首页效果时就想搬到我自己的项目中, 于是百度 google 各种搜索, 好不容易在github上找了一个有点相像的效果,然后ctrl+c 和Ctrl+V 就放到自己项目中了。 但是别人的和你自己想要的终归有点差别, 有时候也有小改动,所以每次我都是摸索着 改一些参数和数值, 带着一种运气的成分调试,最后调成一个自己觉得凑合的效果, 但是始终不知道其中的原理。 在后来工作中我一直没放弃研究这个,经过一段时间的学习和研究,我终于搞懂了这中间的秘密。

CoordinatorLayout 是协调者,用于协调所有子视图之间的动作, 一个子视图发生了改变,想要另一个视图跟着改变,需要怎么做呢?它不能直接告诉它的兄弟视图, 因为事件传递的时候,他不可能得到它兄弟视图的引用。 但是呢?它的父亲可以找到他兄弟视图的引用,所以它把它坐了那些事 告诉它的父亲,然后它的父亲在把它做的这些事, 转告给需要跟着一起改变的兄弟视图。但是怎么改变呢?总不能我走一步,兄弟视图也走一步吧? 那这种一模一样的改变是完全没有意义的。 所以父亲定义了一个抽象的动作类,每次只需要把子视图的做的那些动作,原样转给兄弟视图。 然后兄弟视图自己要做怎样的改变,它自己来定,比如说 你前进一步, 我向左向后各走半步。 这种动作被抽象出来,在开发上叫做解耦。

为了更好的说明CoordinatorLayout与Behavior以及子视图之间的关系,我画了一张图方便大家理解。至于被观察的子视图怎么告诉父视图的,以及父视图又是怎么告诉观察者视图的。 主要跟Behavior 以及 NestedScrollingParent2 、NestedScrollingChild2有关系。如果细心的同学可以看出来,CoordinatorLayout实现了NestedScrollingParent2接口,NestScrollView 实现了NestedScrollingChild2接口。
NestedScrollingParent2源码链接
NestedScrollingChild2源码链接

  /**
   * This interface should be implemented by {@link android.view.ViewGroup ViewGroup} subclasses
   * that wish to support scrolling operations delegated by a nested child view.
   *
   * <p>Classes implementing this interface should create a final instance of a
   * {@link NestedScrollingParentHelper} as a field and delegate any View or ViewGroup methods
   * to the <code>NestedScrollingParentHelper</code> methods of the same signature.</p>
   *
   * <p>Views invoking nested scrolling functionality should always do so from the relevant
   * {@link ViewCompat}, {@link ViewGroupCompat} or {@link ViewParentCompat} compatibility
   * shim static methods. This ensures interoperability with nested scrolling views on all versions
   * of Android.</p>
   */
  public interface NestedScrollingParent2 extends NestedScrollingParent 

实现这个接口可以实现嵌套滚动的分派动作。但是分派动作的功能由NestedScrollingParentHelper代理执行。

  /**
   * This interface should be implemented by {@link View View} subclasses that wish
   * to support dispatching nested scrolling operations to a cooperating parent
   * {@link android.view.ViewGroup ViewGroup}.
   *
   * <p>Classes implementing this interface should create a final instance of a
   * {@link NestedScrollingChildHelper} as a field and delegate any View methods to the
   * <code>NestedScrollingChildHelper</code> methods of the same signature.</p>
   *
   * <p>Views invoking nested scrolling functionality should always do so from the relevant
   * {@link ViewCompat}, {@link ViewGroupCompat} or {@link ViewParentCompat} compatibility
   * shim static methods. This ensures interoperability with nested scrolling views on all versions
   * of Android.</p>
   */
  public interface NestedScrollingChild2 extends NestedScrollingChild

实现了这个接口的子类希望能够把自己的滚动动作分派给父类在协作处理。

所以通常我们使用的是NestedScrollView 作为子类,然后CoordinatorLayout作为父类,然后定义一些子视图并设置好他们的Behavior动作,然后监听NestedScrollView的滚动,做出自己对应的改变。
这里只是做一个简单的说明,后面会结合源码以及Behavior对应的方法,从源码的角度证明上面我描述的这些东西。 当然也会有上面贴图的实战内容。
谢谢阅读欢迎大家批评指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章