CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout
CoordinatorLayout
CoordinatorLayout
相当于一个功能更加强大的FrameLayout
,主要有两个用途:
- 作为顶层布局;
- 作为协调与子布局之间交互的容器。
通过为子布局指定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
会覆盖CoordinatorLayout
的onMeasureChild
行为。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进行交互,必须是实现了NestedScrollingChild2
或NestedScrollingChild
的View,且必须调用dispatchNestedScroll
,否则无法触发behavior的行为。例如滚动视图为ScrollView
或ListView
,由于没有实现相应接口,所以也无法触发behavior,改为相应的NestedScrollView
或RecyclerView
则没问题。
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:contentScrim
或setContentScrim
设置CollapsingToolbarLayout滚动打到某个阈值时显示的背景。app:statusBarScrim
或setStatusBarScrim
设置CollapsingToolbarLayout滚动打到某个阈值时Status bar显示的背景。collapseMode
CollapsingToolbarLayout的子View可以有以下两种折叠模式可以选择,可通过app:layout_collapseMode
或setCollapseMode
进行设置:parallax模式:
COLLAPSE_MODE_PARALLAX
滚动时,设置该模式的子View会跟着Scroll View一起滚动,通常和视差因子app:layout_collapseParallaxMultiplier
或setParallaxMultiplier
配合使用,视差因子范围为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:collapsedTitleGravity
或setCollapsedTitleGravity
设置折叠时title文本的位置。app:collapsedTitleTextAppearance
或setCollapsedTitleTextAppearance
设置折叠时title文本的样式。app:expandedTitleTextAppearance
等expanded属性为设置展开时title文本的样式。app:scrimAnimationDuration
或setScrimAnimationDuration
设置折叠时所显示的背景(scrims)时长。app:scrimVisibleHeightTrigger
或setScrimVisibleHeightTrigger
设置特定高度用于控制折叠背景的可见性,当CollapsingToolbarLayout的可见高度小于该值时,折叠背景(scrims)可见,否则不可见。
CoordinatorLayout + AppBarLayout + CollapsingToolbarLayout相关推荐
- Android之解决CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+RecyclerView里面再嵌套RecyclerView滑动颤抖问题
1 问题 主页面用的是这种结构 CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+RecyclerView(里面再嵌套RecyclerVie ...
- CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar组合悬浮置顶的效果
最近由于公司业务需求的增加,产品部门给了一个新功能 类似于掌上英雄联盟的效果,但是实际功能比掌上英雄联盟要多,可以看一下掌上英雄联盟的效果.效果图如下: 我们产品的功能需求是,当向上滑动的时候名称呈渐 ...
- CoordinatorLayout/AppbarLayout/CollapsingToolbarLayout的配合使用,Toolbar特效
上面这个特效就是Toolbar与CoordinatorLayout/AppbarLayout/CollapsingToolbarLayout配合后的效果:这几个View单独使用没什么效果,一般都是要互 ...
- #Android笔记# 超级足球app 开发总结(三)—— CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout实现两种折叠效果
最近利用业余时间,开发了一款基于懂球帝接口数据的足球资讯app,整体的UI也是仿照懂球帝设计的.这是一个比较综合的项目,用到了不少以前没用过的组件和api,而且产生了很多新的开发思路,有些实现方式也是 ...
- CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar实现渐变透明的状态栏
在之前的一篇博文里面我已经说明了CoordinatorLayout使用过程中遇到的问题,之后又发现结合CollapsingToolbarLayout使用时的另一个问题.CollapsingToolba ...
- coordinatorlayout_一篇文章学会Coordinatorlayout+AppbarLayout
点击上方蓝字关注 ?? 来源: 奔跑吧李博 https://www.jianshu.com/p/cd93da2b7a24 前言 现如今,折叠式布局在App中相当常见,给人一种科技感,充满良好的用户体 ...
- 纯享版-Android AppBarLayout + CollapsingToolbarLayout丝滑自动折叠、吸顶
使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+TabLayout+ViewPager,实现tab滑动吸顶效果简直完爆ScrollVi ...
- 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用...
Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...
- CoordinatorLayout+AppBarLayout顶部栏吸顶效果
看需求/效果 如果要实现如下效果,顶部标题栏随着上滑隐藏,且能显示缩略文字标题,那么就要用到CoordinatorLayout+几个layout了. 控件简介 CoordinatorLayout遵循M ...
最新文章
- 万年历java课程设计报告_java万年历课程设计报告2010
- 乌鲁木齐计算机老师待遇,如果月薪超过10000元,安排去新疆当教学教师,愿意吗?...
- All in会员经济的知乎,能否实现商业化大跃进?
- Codeforces Round #131 (Div. 2)------AB
- vc2010+openCV1.0实现将指定目录下的所有jpg文件缩放后存放到目标文件夹
- .NET深入学习笔记(4):深拷贝与浅拷贝(Deep Copy and Shallow Copy)
- Nginx 附录C 模块编译,调试与测试
- hdu 3068 最长回文 (Manacher算法求最长回文串)
- OFFICE必学技巧开篇
- mac终端 install_Mac常用终端命令
- php 随机输出字符串,如何使用PHP生成随机字符串
- java修改jpg图片、mp3音频文件的后缀名
- 给ftp服务器创建文件夹,ftp服务器上创建文件夹
- oracle cube语法,oracle Rollup 和 Cube用法
- 摄像机镜头的计算方法
- 口罩机远程监控运维解决方案
- HMI-64-【多媒体】Ui全部制作完成
- 操作系统-添加文件加密系统
- 【hbase问题】org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not run
- Scrum立会报告+燃尽图(Beta阶段第二周第六次)