CoordinatorLayout

CoordinatorLayout相当于一个功能更加强大的FrameLayout,主要有两个用途:

  1. 作为顶层布局;
  2. 作为协调与子布局之间交互的容器。

通过为子布局指定Behaviors,可以为子布局之间提供不同的交互。CoordinatorLayout的子布局可以通过实现CoordinatorLayout.AttachedBehavior接口为自身添加默认行为(behavior)。简单来说,CoordinatorLayout相当于一个第三方,通过Behaviors属性对子布局之间进行行为交互控制。具体如何控制如下:

##CoordinatorLayout
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {...for (int i = 0; i < childCount; i++) {final View child = mDependencySortedChildren.get(i);if (child.getVisibility() == GONE) {// If the child is GONE, skip...continue;}final LayoutParams lp = (LayoutParams) child.getLayoutParams();...final Behavior b = lp.getBehavior();if (b == null || !b.onMeasureChild(this, child, childWidthMeasureSpec, keylineWidthUsed,childHeightMeasureSpec, 0)) {onMeasureChild(child, childWidthMeasureSpec, keylineWidthUsed,childHeightMeasureSpec, 0);}...}...
}##CoordinatorLayout.Behavior<V extends android.view.View>
/*** @return true if the Behavior measured the child view, false if the CoordinatorLayout should perform its default measurement*/
public boolean onMeasureChild(CoordinatorLayout parent, V child, int parentWidthMeasureSpec, int widthUsed,int parentHeightMeasureSpec, int heightUsed) {return false;
}

可见CoordinatorLayout是通过判断子布局是否存在Behavior或存在的Behavior是否measure了子布局来判读CoordinatorLayout自身是否需要执行自己的measure,即Behavior会覆盖CoordinatorLayoutonMeasureChild行为。layout时同理:

##CoordinatorLayout
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {final int layoutDirection = ViewCompat.getLayoutDirection(this);final int childCount = mDependencySortedChildren.size();for (int i = 0; i < childCount; i++) {final View child = mDependencySortedChildren.get(i);if (child.getVisibility() == GONE) {// If the child is GONE, skip...continue;}final LayoutParams lp = (LayoutParams) child.getLayoutParams();final Behavior behavior = lp.getBehavior();if (behavior == null || !behavior.onLayoutChild(this, child, layoutDirection)) {onLayoutChild(child, layoutDirection);}}
}##CoordinatorLayout.Behavior<V extends android.view.View>
/* * @return true if the Behavior performed layout of the child view, false to request default layout behavior* /
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {return false;
}

当然上面只是通过Behavior去控制子布局的行为,但是在执行重绘之前,CoordinatorLayout和子布局又是如何协调滚动或滑动等效果的呢?

##CoordinatorLayout.Behavior<V extends android.view.View>
/* * @return true if this Behavior would like to intercept and take over the event stream.* The default always returns false.*/
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {return false;
}/** @return true if this Behavior handled this touch event and would like to continue * receiving events in this stream. The default always returns false.*/
public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent ev) {return false;
}

首先是Behavior可以选择是否拦截消费触摸事件。

public class CoordinatorLayout extends ViewGroup implements NestedScrollingParent2 {...
}

首先是CoordinatorLayout实现了NestedScrollingParent2接口,通过该接口获取子View滚动的相关参数,再通过behavior传到对各个子view中,那些包含了behavior的子View便会消费掉相关参数,处理滚动事件,达到交互目的。所以这就要求CoordinatorLayout的子布局要想实现通过behavior进行交互,必须是实现了NestedScrollingChild2NestedScrollingChild的View,且必须调用dispatchNestedScroll,否则无法触发behavior的行为。例如滚动视图为ScrollViewListView,由于没有实现相应接口,所以也无法触发behavior,改为相应的NestedScrollViewRecyclerView则没问题。

AppBarLayout

AppBarLayout是一个实现了很多Material Design风格的app bar设计概论(即滚动手势)的vertical LinearLayout,其子布局应该通过setScrollFlags(int)app:layout_scrollFlags设置其behavior,包括以下五种flag:

  • SCROLL_FLAG_SCROLL:该View随着滚动事件滚动。以下其他属性若要生效必须得先设置该属性,即该属性必须优先设置。

  • SCROLL_FLAG_EXIT_UNTIL_COLLAPSED:向上滚动时,该View先滚动退出到其最小高度值(setMinimumHeight),然后Scroll View开始滚动,即该View优先滚动但不会完全退出屏幕。

  • SCROLL_FLAG_ENTER_ALWAYS: 向下滚动时,优先滚动该View,再滚动Scroll View。 通常也称这种模式为’quick return’。

  • SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED:SCROLL_FLAG_ENTER_ALWAYS的扩展属性,向下滚动时,该View先滚动显示到其最小高度(setMinimumHeight),然后Scroll View开始滚动,直到Scroll View滚到边界时,该View再继续滚动,直到完全显示。

  • SCROLL_FLAG_SNAP:该View的一个吸附效果,即当滚动时,该View会跟着滚动,然后松开手指时,若该View显示区域大于隐藏区域,则整个View完全显示,反之则完全退出屏幕。也就是说,设置了该属性的View,要么完全显示,要么完全滚出屏幕。

AppBarLayout一般用于作为CoordinatorLayout的直接子View,当作为其他ViewGroup的子View时,会导致其大部分功能无法使用。同时,AppBarLayout还需要一个可滚动的兄弟View,即跟其处于同一层并列的CoordinatorLayout直接子View。同时,还需要为该滚动视图的behavior设置AppBarLayout.ScrollingViewBehavior的实例。如:app:layout_behavior="@string/appbar_scrolling_view_behavior"

CollapsingToolbarLayout

CollapsingToolbarLayout是一个实现了折叠效果的FrameLayout,主要作为AppBarLayout的直接子View,用于控制自身子View的折叠。包含以下属性:

  • app:contentScrimsetContentScrim
    设置CollapsingToolbarLayout滚动打到某个阈值时显示的背景。

  • app:statusBarScrimsetStatusBarScrim
    设置CollapsingToolbarLayout滚动打到某个阈值时Status bar显示的背景。

  • collapseMode
    CollapsingToolbarLayout的子View可以有以下两种折叠模式可以选择,可通过app:layout_collapseModesetCollapseMode进行设置:

    parallax模式COLLAPSE_MODE_PARALLAX
    滚动时,设置该模式的子View会跟着Scroll View一起滚动,通常和视差因子app:layout_collapseParallaxMultipliersetParallaxMultiplier配合使用,视差因子范围为0 ~ 1。

    Pinned模式COLLAPSE_MODE_PIN
    一般设置给Toolbar,如果是Toolbar,则无论滚动时还是滚动结束,整个Toolbar都会完整地留在屏幕上,但若不是Toolbar,则滚动到该子View的高度之前,该View不会有变化,到达该View后,该View便会逐渐缩小至CollapsingToolbarLayout的最小高度(若最小高度小于该View的高度),同时,若是设置了收缩的背景(scrims),则该背景还会覆盖该View。所以最好还是将需要设置该属性的放到Toolbar里,然后将该属性设置给Toolbar。

    普通模式COLLAPSE_MODE_OFF
    若是两种模式都不想用,可以不设置collapseMode或者将其设置为COLLAPSE_MODE_OFF,该模式的子View将表现为没有设置折叠行为的View。

  • app:collapsedTitleGravitysetCollapsedTitleGravity
    设置折叠时title文本的位置。

  • app:collapsedTitleTextAppearancesetCollapsedTitleTextAppearance
    设置折叠时title文本的样式。

  • app:expandedTitleTextAppearance等expanded属性为设置展开时title文本的样式。

  • app:scrimAnimationDurationsetScrimAnimationDuration
    设置折叠时所显示的背景(scrims)时长。

  • app:scrimVisibleHeightTriggersetScrimVisibleHeightTrigger
    设置特定高度用于控制折叠背景的可见性,当CollapsingToolbarLayout的可见高度小于该值时,折叠背景(scrims)可见,否则不可见。

CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout相关推荐

  1. Android之解决CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+RecyclerView里面再嵌套RecyclerView滑动颤抖问题

    1 问题 主页面用的是这种结构 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+RecyclerView(里面再嵌套RecyclerVie ...

  2. CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar组合悬浮置顶的效果

    最近由于公司业务需求的增加,产品部门给了一个新功能 类似于掌上英雄联盟的效果,但是实际功能比掌上英雄联盟要多,可以看一下掌上英雄联盟的效果.效果图如下: 我们产品的功能需求是,当向上滑动的时候名称呈渐 ...

  3. CoordinatorLayout/AppbarLayout/CollapsingToolbarLayout的配合使用,Toolbar特效

    上面这个特效就是Toolbar与CoordinatorLayout/AppbarLayout/CollapsingToolbarLayout配合后的效果:这几个View单独使用没什么效果,一般都是要互 ...

  4. #Android笔记# 超级足球app 开发总结(三)—— CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout实现两种折叠效果

    最近利用业余时间,开发了一款基于懂球帝接口数据的足球资讯app,整体的UI也是仿照懂球帝设计的.这是一个比较综合的项目,用到了不少以前没用过的组件和api,而且产生了很多新的开发思路,有些实现方式也是 ...

  5. CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar实现渐变透明的状态栏

    在之前的一篇博文里面我已经说明了CoordinatorLayout使用过程中遇到的问题,之后又发现结合CollapsingToolbarLayout使用时的另一个问题.CollapsingToolba ...

  6. coordinatorlayout_一篇文章学会Coordinatorlayout+AppbarLayout

    点击上方蓝字关注 ?? 来源:  奔跑吧李博 https://www.jianshu.com/p/cd93da2b7a24 前言 现如今,折叠式布局在App中相当常见,给人一种科技感,充满良好的用户体 ...

  7. 纯享版-Android AppBarLayout + CollapsingToolbarLayout丝滑自动折叠、吸顶

    使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+TabLayout+ViewPager,实现tab滑动吸顶效果简直完爆ScrollVi ...

  8. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用...

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  9. CoordinatorLayout+AppBarLayout顶部栏吸顶效果

    看需求/效果 如果要实现如下效果,顶部标题栏随着上滑隐藏,且能显示缩略文字标题,那么就要用到CoordinatorLayout+几个layout了. 控件简介 CoordinatorLayout遵循M ...

最新文章

  1. 万年历java课程设计报告_java万年历课程设计报告2010
  2. 乌鲁木齐计算机老师待遇,如果月薪超过10000元,安排去新疆当教学教师,愿意吗?...
  3. All in会员经济的知乎,能否实现商业化大跃进?
  4. Codeforces Round #131 (Div. 2)------AB
  5. vc2010+openCV1.0实现将指定目录下的所有jpg文件缩放后存放到目标文件夹
  6. .NET深入学习笔记(4):深拷贝与浅拷贝(Deep Copy and Shallow Copy)
  7. Nginx 附录C 模块编译,调试与测试
  8. hdu 3068 最长回文 (Manacher算法求最长回文串)
  9. OFFICE必学技巧开篇
  10. mac终端 install_Mac常用终端命令
  11. php 随机输出字符串,如何使用PHP生成随机字符串
  12. java修改jpg图片、mp3音频文件的后缀名
  13. 给ftp服务器创建文件夹,ftp服务器上创建文件夹
  14. oracle cube语法,oracle Rollup 和 Cube用法
  15. 摄像机镜头的计算方法
  16. 口罩机远程监控运维解决方案
  17. HMI-64-【多媒体】Ui全部制作完成
  18. 操作系统-添加文件加密系统
  19. 【hbase问题】org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not run
  20. Scrum立会报告+燃尽图(Beta阶段第二周第六次)

热门文章

  1. UOS其他分区及移动硬盘带锁问题处理
  2. Win11 与 macOS 12 界面对比
  3. WIN10 DOS命令
  4. 5 爬虫 异步协程 梨视频
  5. (附源码)springboot电子病历管理APP 毕业设计 010350
  6. 关于睡眠唤醒实现开机功能
  7. 使用Pageoffice打开Office word报错0x80040154问题或者卸除WPS后Microsoft Office图标无法显示问题
  8. 计算机基础一:IP地址与域名解析
  9. 用狄克斯特拉算法计算带权最短路径
  10. iOS 绘图(drawrect)图片裁剪的红色框框