博客
关于我
Android Support Design 中 CoordinatorLayout 与 Behaviors 初探
阅读量:127 次
发布时间:2019-02-27

本文共 2676 字,大约阅读时间需要 8 分钟。

在Android M Preview发布后,我们获得了一个新的support library —— Android Design Support Library 用来实现Google的Material Design 提供了一系列符合设计标准的控件。

其中有众多的控件,其中最复杂,功能最强大的就是CoordinatorLayout,顾名思义,它是用来组织它的子views之间协作的一个父view。CoordinatorLayout默认情况下可理解是一个FrameLayout,它的布局方式默认是一层一层叠上去。

那么,CoordinatorLayout的神奇之处就在于Behavior对象了。
看下CoordinatorLayout.Behavior对象的 Overview

Interaction behavior plugin for child views of CoordinatorLayout.A Behavior implements one or more interactions that a user can take on a child view. These interactions may include drags, swipes, flings, or any other gestures.

可知Behavior对象是用来给CoordinatorLayout的子view们进行交互用的。

Behavior接口拥有很多个方法,我们拿AppBarLayout为例。AppBarLayout中有两个Behavior,一个是拿来给它自己用的,另一个是拿来给它的兄弟结点用的,我们重点关注下AppBarLayout.ScrollingViewBehavior这个类。

我们看下这个类中的以下方法

0. dependency

public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency)     {    return dependency instanceof AppBarLayout;}

这个方法告诉CoordinatorLayout,这个view是依赖AppBarLayout的,后续父亲可以利用这个方法,查找到这个child所有依赖的兄弟结点。

1. measure

public boolean onMeasureChild(CoordinatorLayout parent, View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

这个是CoordinatorLayout在进行measure的过程中,利用Behavior对象对子view进行大小测量的一个方法。

在这个方法内,我们可以通过parent.getDependencies(child);这个方法,获取到这个child依赖的view,然后通过获取这个child依赖的view的大小来决定自身的大小。

2. layout

public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection)

这个方法是用来子view用来布局自身使用,如果依赖其他view,那么系统会首先调用

public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)

这个方法,可以在这个回调中记录dependency的一些位置信息,在onLayoutChild中利用保存下来的信息进行计算,然后得到自身的具体位置。

3. nested scroll

public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes)public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target)

这几个方法是不是特别熟悉?我在Android嵌套滑动机制(NestedScrolling) 介绍过,这几个方法刚好是NestedScrollingParent的方法,也就是对CoodinatorLayout进行的一个代理(Proxy),即CoordinatorLayout自己不对这些消息进行处理,而是传递给子view的Behavior,进行处理。利用这样的方法,实现了view和view之间的交互和视觉的协同(布局、滑动)。

总结

可以看到CoodinatorLayout给我们实现了一个可以被子view代理实现方法的一个布局。这和传统的ViewGroup不同,子view从此知道了彼此之间的存在,一个子view的变化可以通知到另一个子view。CoordinatorLayout所做的事情就是当成一个通信的桥梁,连接不同的view。使用Behavior对象进行通信。

我们具体的实现可以参照 Android官方文档告诉我们的每一个方法的作用 进行重写,实现自己想要的各种复杂的功能。

有了这么一套机制,想实现组件之间的交互,就更加方便快捷啦~


转载地址:http://dxgf.baihongyu.com/

你可能感兴趣的文章
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>