android coordinator layoutlayout.behaviorfirst怎么移动.second都会在他下面

android CoordinatorLayout使用
一、CoordinatorLayout有什么作用
CoordinatorLayout作为&super-powered FrameLayout&基本实现两个功能:
1、作为顶层布局
2、调度协调子布局
CoordinatorLayout使用新的思路通过协调调度子布局的形式实现触摸影响布局的形式产生动画效果。CoordinatorLayout通过设置子View的 Behaviors来调度子View。(Support V7)提供了AppBarLayout.Behavior, AppBarLayout.ScrollingViewBehavior, FloatingActionButton.Behavior, SwipeDismissBehavior 等。
使用CoordinatorLayout需要在Gradle加入Support Design Library:
compile 'com.android.support:design:22.2.1'
二、CoordinatorLayout与FloatingActionButton
定义布局文件:
CoordinatorLayout作为&super-powered FrameLayout&,设置子视图的android:layout_gravity属性控制位置。
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Snackbar.make(view,FAB,Snackbar.LENGTH_LONG)
.setAction(cancel, new View.OnClickListener() {
public void onClick(View v) {
//这里的单击事件代表点击消除Action后的响应事件
FloatingActionButton是最简单的使用CoordinatorLayout的例子,FloatingActionButton默认使用FloatingActionButton.Behavior。
三、CoordinatorLayout与AppBarLayout
3.1 AppBarLayout嵌套TabLayout
布局文件代码:
效果显示,视图滚动时,Toolbar会隐藏,这个效果是 Support Library里面,新增的CoordinatorLayout, AppBarLayout实现的。通过AppBarLayout的子视图的属性控制。观察AppBarLayout的子布局,Toobar有app:layout_scrollFlags属性,这就是控制滑动时视图效果的属性。app:layout_scrollFlags有四个值:
scroll: 所有想滚动出屏幕的view都需要设置这个flag, 没有设置这个flag的view将被固定在屏幕顶部。例如,TabLayout 没有设置这个值,将会停留在屏幕顶部。 enterAlways: 设置这个flag时,向下的滚动都会导致该view变为可见,启用快速&返回模式&。 enterAlwaysCollapsed: 当你的视图已经设置minHeight属性又使用此标志时,你的视图只能已最小高度进入,只有当滚动视图到达顶部时才扩大到完整高度。 exitUntilCollapsed: 滚动退出屏幕,最后折叠在顶端。
为了ToolBar可以滚动,CoordinatorLayout里面,放一个带有可滚动的View.如上的例子,放的是ViewPager,而ViewPager里面是放了RecylerView的,即是可以滚动的View。CoordinatorLayout包含的子视图中带有滚动属性的View需要设置app:layout_behavior属性。例如,示例中Viewpager设置了此属性。
app:layout_behavior=@string/appbar_scrolling_view_behavior
为了使得Toolbar有滑动效果,必须做到如下三点:
1. CoordinatorLayout作为布局的父布局容器。
2. 给需要滑动的设置 app:layout_scrollFlags=&scroll|enterAlways& 属性。
3. 给滑动的组件设置app:layout_behavior属性
3.2 AppBarLayout嵌套CollapsingToolbarLayout
这种效果在详情页面用的较多,展示个性化内容,图像有强烈的吸引力。这个效果重点使用了CollapsingToolbarLayout 。
CollapsingToolbarLayout可实现Toolbar的折叠效果。CollapsingToolbarLayout的子视图类似与LinearLayout垂直方向排放。
CollapsingToolbarLayout 提供以下属性和方法是用:
1. Collapsing title:ToolBar的标题,当CollapsingToolbarLayout全屏没有折叠时,title显示的是大字体,在折叠的过程中,title不断变小到一定大小的效果。你可以调用setTitle(CharSequence)方法设置title。
2. Content scrim:ToolBar被折叠到顶部固定时候的背景,你可以调用setContentScrim(Drawable)方法改变背景或者 在属性中使用 app:contentScrim=&?attr/colorPrimary&来改变背景。
3. Status bar scrim:状态栏的背景,调用方法setStatusBarScrim(Drawable)。还没研究明白,不过这个只能在Android5.0以上系统有效果。
4. Parallax scrolling children:CollapsingToolbarLayout滑动时,子视图的视觉差,可以通过属性app:layout_collapseParallaxMultiplier=&0.6&改变。值de的范围[0.0,1.0],值越大视察越大。
5. CollapseMode :子视图的折叠模式,在子视图设置,有两种&pin&:固定模式,在折叠的时候最后固定在顶端;&parallax&:视差模式,在折叠的时候会有个视差折叠的效果。我们可以在布局中使用属性app:layout_collapseMode=&parallax&来改变。
CoordinatorLayout 还提供了一个 layout_anchor 的属性,连同 layout_anchorGravity 一起,可以用来放置与其他视图关联在一起的悬浮视图(如 FloatingActionButton)。本例中使用FloatingActionButton。
通过下面的参数设置了FloatingActionButton的位置,两个属性共同作用使得FAB 浮动按钮也能折叠消失,展现。
app:layout_anchor=@id/appbar
app:layout_anchorGravity=bottom|right|end
使用CollapsingToolbarLayout实现折叠效果,需要注意3点
1. AppBarLayout的高度固定
2. CollapsingToolbarLayout的子视图设置layout_collapseMode属性
3. 关联悬浮视图设置app:layout_anchor,app:layout_anchorGravity属性
四、自定义behavior
CoordinatorLayout功能如此强大,而他的神奇之处在于Behavior对象,CoordinatorLayout自己并不控制View,所有的控制权都在Behavior。前面写到了FloatingActionButton.Behavior,AppBarLayout.Behavior, AppBarLayout.ScrollingViewBehavior。 AppBarLayout中有两个Behavior,一个是拿来给它自己用的,另一个是拿来给它的兄弟结点用的。这些Behavior实现了复杂的控制功能。系统的Behavior毕竟有限,我们可以通过自定义的方式来实现自己的Behavior。
通过 CoordinatorLayout.Behavior(YourView.Behavior.class) 来定义自己的Behavior,并在layout 文件中设置 app:layout_behavior=&com.example.app.YourView$Behavior& 来达到效果。
自定义Behavior 需要重写两个方法:
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency)
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency)
如下面的例子,实现了点击FloatingActionButton点击旋转90度,并适配Snackbar。
public class RotateBehavior
extends CoordinatorLayout.Behavior {
private static final String TAG = RotateBehavior.class.getSimpleName();
public RotateBehavior() {
public RotateBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
return dependency instanceof Snackbar.SnackbarL
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
float translationY = getFabTranslationYForSnackbar(parent, child);
float percentComplete = -translationY / dependency.getHeight();
child.setRotation(-90 * percentComplete);
child.setTranslationY(translationY);
private float getFabTranslationYForSnackbar(CoordinatorLayout parent,
FloatingActionButton fab) {
float minOffset = 0;
final List dependencies = parent.getDependencies(fab);
for (int i = 0, z = dependencies.size(); i & i++) {
final View view = dependencies.get(i);
if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(fab, view)) {
minOffset = Math.min(minOffset,
ViewCompat.getTranslationY(view) - view.getHeight());
return minO
综上,基本覆盖了CoordinatorLayout的使用方式。android coordinatorlayout.behavior仿饿了么_百度知道
android coordinatorlayout.behavior仿饿了么
我有更好的答案
As a top-level application decor or chrome layoutAs a container for a specific interaction with one or more child views官方对Behavior的描述是这样的:Interaction behavior plugin for child views of CoordinatorLayout.简单来说,CoordinatorLayout是用来协调其子view们之间动作的一个父view,而
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。谈谈Material Design之CoordinatorLayout
本文主要介绍一下如何使用CoordinatorLayout
先看看官方是怎么介绍Material Design的
We challenged ourselves to create a visual language for our users that synthesizes the classic principles of good design with the innovation and possibility of technology and science. This is material design. This spec is a living document that will be updated as we continue to develop the tenets and specifics of material design.
通过上面的这段英文相信大家对Material Design或多或少都有一定的认识了吧!大概的意思也就是google努力的帮你设计出一套很好视图设计规范,你按这个规范来咯~~~
我觉得我们需要关注的控件也就下面4个吧~
SnackBar:一个类似于Toast的控件,下文会提及 FloatingActionButton:悬浮按钮 CoordinatorLayout:也就是本篇博文的猪脚咯~~ Tablayout:本篇不聊
讲了这么多废话,那么我们正式进入今天的主题吧!
CoordinatorLayout 实现了多种Material Design中提到的滚动效果。目前这个框架提供了几种不用写动画代码就能工作的方法,这些效果包括:
当snackbar显示的时候,浮动按钮上移,给snackbar留出位置:效果如下
扩展或者缩小Toolbar或者头部,让主内容区域有更多的空间:效果如下
控制哪个view应该扩展还是收缩,以及其显示大小比例,包括视差滚动效果动画。
自行引入Design Support Library,不清楚请自行网补
首先,我们看第一种效果
嘻嘻,今天猪脚终于亮相了,首先我们使用了CoordinatorLayout作为根布局,CoordinatorLayout可以用来配合浮动操作按钮的 layout_anchor 和 layout_gravity属性创造出浮动效果,只要使用CoordinatorLayout作为基本布局,将自动产生向上移动的动画。浮动操作按钮有一个 默认的 behavior来检测Snackbar的添加并让其在Snackbar之上呈现上移与Snackbar等高的动画。
咳咳,这个我相信大家都能懂,那么我们来说说FloatingActionButton这个控件。
当我们的项目需要一个圆形的Button, 你可能会想到用自定义Shape的方式去做,但那样文本的显示不好居中,这时估计就想到用自定义控件去解决了。以上都是废话,google给我们提供了FloatingActionButton可以轻松的创建圆形Button,而且更牛x的是FloatingActionButton具有更绚丽的效果。
FloatingActionButton继承自ImageView,所以你懂的,ImageView的属性我们阔以直接拿来用咯,so。。如果我们需要整个圆形的ImageView也当然阔以直接用这个咯~~
FloatingActionButton的属性主要有
app:backgroundTint是指定默认的背景颜色 app:rippleColor是指定点击时的背景颜色 app:borderWidth border的宽度 app:fabSize是指FloatingActionButton的大小,可选normal|mini app:elevation 可以看出该空间有一个海拔的高度 app:pressedTranslationZ 哈,按下去时的z轴的偏移
传说中的一图胜千言
卡卡的 凑合看吧!!
第二种效果 Toolbar的扩展与收缩
先看效果吧
首先你必须保证用toolbar替代actionbar,至于toolbar的具体使用,也不是本篇的重点,不清楚的童鞋阔以自行网补(话说这博客到底有木有干货~~啥都要网补)
接下来,我们必须使用一个容器布局:AppBarLayout来让Toolbar响应滚动事件。请注意:AppBarLayout必须是CoordinatorLayout的直接子View。
然后,我们需要定义AppBarLayout与滚动视图之间的联系。在RecyclerView或者任意支持嵌套滚动的view(比如NestedScrollView)上添加app:layout_behavior。support library包含了一个特殊的字符串资源@string/appbar_scrolling_view_behavior(此处是反射),它和AppBarLayout.ScrollingViewBehavior相匹配,用来通知AppBarLayout 这个特殊的view何时发生了滚动事件。
这个behavior需要设置在触发事件(滚动)的view之上。(注意上面提到的嵌套滚动~~~~比如我们常用的scrollView、listview都是属于不支持嵌套滚动的)
当CoordinatorLayout发现RecyclerView中定义了这个属性,它会搜索自己所包含的其他view,看看是否有view与这个behavior相关联。AppBarLayout.ScrollingViewBehavior描述了RecyclerView与AppBarLayout之间的依赖关系。RecyclerView的任意滚动事件都将触发AppBarLayout或者AppBarLayout里面view的改变。
细心的你不知道是否已经发现~额 toolbar有
app:layout_scrollFlags=&scroll|enterAlways&
这样一条属性。app:layout_scrollFlags,什么玩意儿,听我慢慢道来。前面我们提到RecyclerView的任意滚动事件都将触发AppBarLayout或者AppBarLayout里面view的改变。那么,AppBarLayout里面的子View到底以怎么样的方式进行滚动呢?此时这个属性就起作用了!!!
那么我们看看这个属性的属性值都用哪些
scroll 谁要滚出屏幕,谁就设置这个值 enterAlways 其他向上滑动时,可以立即显示出来 exitUntilCollapsed 将关闭滚动直到它被折叠起来(有 minHeight) 并且一直保持这样 enterAlwaysCollapsed 定义了 View 是如何回到屏幕的,当你的 view 已经声明了一个最小高度(minHeight) 并且你使用了这个标志,你的 View 只有在回到这个最小的高度的时候才会展开,只有当 view 已经到达顶部之后它才会重新展开全部高度。
第三种,折叠效果
可以看到,我们将image和toolbar使用CollapsingToolbarLayout包裹起来了
通常,我们我们都是设置Toolbar的title,而现在,我们需要把title设置在CollapsingToolBarLayout上,而不是Toolbar。
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(&Title&);
该控件的的子view必须要有Toolbar,他的作用就是提供一个可折叠的标题栏。通常情况下,该控件会和上面我们说的一些控件搭配使用,达到一种固定的效果。
我们先分析一下上面这段布局,最直观的,可以看到有一个行为上的协作, 那肯定需要CoordinatorLayout这个控件,而且还会有一个滑动的效果,那肯定是AppBarLayout啦,当然,细心的朋友可能还会看出来,那个图片和Toolbar会有一种视差的效果,而且整个banner部分是一种折叠的效果,这就需要CollapsingToolbarLayout这个控件了。
AppBarLayout通过CollapsingToolBarLayout把ImageView和Toolbar作为整个app的标题栏,而且表示滚动标识的app:layout_scrollFlags=&scroll|exitUntilCollapsed&也是赋值给了CollapsingToolbarLayout,mageView有一条属性app:layout_collapseMode=&parallax&表示这个ImageView以视差的形式折叠(效果上看着貌似就是有点小偏移)。 Toolbar的app:layout_collapseMode=&pin&表示Toolbar在折叠的过程中会停靠顶部(pin的意思是钉住)。 这样CollapsingToolBarLayout就没有全部滚出界面。
如果你希望滚动的时候渐变的颜色跟随的是图片而不是蓝色渐变,在java代码中使用如下代码
final CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.ctl);
ctl.setTitle(&Temperate_box&);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.banner);
Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
Palette.Swatch swatch = palette.getDarkMutedSwatch();
ctl.setContentScrimColor(swatch.getRgb());
ok,打完收工~~~~&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
关于 CoordinatorLayout 与 Behavior 的一点分析
摘要:Behavior是Android新出的Design库里新增的布局概念。Behavior只有是CoordinatorLayout的直接子View才有意义。可以为任何View添加一个Behavior。Behavior是一系列回调。让你有机会以非侵入的为View添加动态的依赖布局,和处理父布局(CoordinatorLayout)滑动手势的机会。不过官方只有少数几个Behavior的例子。对于理解Behavior实在不易。开发过程中也是很多坑,下面总结一下CoordinatorLa
Behavior是Android新出的Design库里新增的布局概念。Behavior只有是CoordinatorLayout的直接子View才有意义。可以为任何View添加一个Behavior。
Behavior是一系列回调。让你有机会以非侵入的为View添加动态的依赖布局,和处理父布局(CoordinatorLayout)滑动手势的机会。不过官方只有少数几个Behavior的例子。对于理解Behavior实在不易。开发过程中也是很多坑,下面总结一下CoordinatorLayout与Behavior。
首先自定义一个Behavior。
public class MyBehavior extends CoordinatorLayout.Behavior{
public MyBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
public class MyBehavior extends CoordinatorLayout.Behavior{public MyBehavior(Context context, AttributeSet attrs) {super(context, attrs);}}
一定要重写这个构造函数。因为CoordinatorLayout源码中parseBehavior()函数中直接反射调用这个构造函数。
static final Class&?&[] CONSTRUCTOR_PARAMS = new Class&?&[] {
Context.class,
AttributeSet.class
static final Class&?&[] CONSTRUCTOR_PARAMS = new Class&?&[] {Context.class,AttributeSet.class};
下面反射生成Behavior实例在实例化CoordinatorLayout.LayoutParams时:
final Class&Behavior& clazz = (Class&Behavior&) Class.forName(fullName, true,
context.getClassLoader());
c = clazz.getConstructor(CONSTRUCTOR_PARAMS);
c.setAccessible(true);
constructors.put(fullName, c);
return c.newInstance(context, attrs)
final Class&Behavior& clazz = (Class&Behavior&) Class.forName(fullName, true, context.getClassLoader());c = clazz.getConstructor(CONSTRUCTOR_PARAMS);c.setAccessible(true);constructors.put(fullName, c);return c.newInstance(context, attrs)在任意View中添加:
app:layout_behavior=“你的Behavior包含包名的类名”
app:layout_behavior=“你的Behavior包含包名的类名”
然后CoordinatorLayout就会反射生成你的Behavior。
另外一种方法如果你的自定义View默认使用一个Behavior。在你的自定义View类上添加@DefaultBehavior(你的Behavior.class)这句注解。你的View就默认使用这个Behavior。就像AppBarLayout一样。
@DefaultBehavior(AppBarLayout.Behavior.class)
public class AppBarLayout extends LinearLayout {}
@DefaultBehavior(AppBarLayout.Behavior.class)public class AppBarLayout extends LinearLayout {}
生成Behavior后第一件事就是确定依赖关系。重写Behavior的这个方法来确定你依赖哪些View。
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency.getId() == R.id.
@Overridepublic boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {return dependency.getId() == R.id.}child 是指应用behavior的View ,dependency 担任触发behavior的角色,并与child进行互动。
确定你是否依赖于这个View。CoordinatorLayout会将自己所有View遍历判断。如果确定依赖。这个方法很重要。当所依赖的View变动时会回调这个方法。
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
@Overridepublic boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {}下面这个例子:
&declare-styleable name=&Follow&&
&attr name=&target& format=&reference&/&
&/declare-styleable&
&declare-styleable name=&Follow&&&attr name=&target& format=&reference&/&&/declare-styleable&先自定义target这个属性。
public class FollowBehavior extends CoordinatorLayout.Behavior {
private int targetId;public FollowBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Follow);
for (int i = 0; i & a.getIndexCount(); i++) {
int attr = a.getIndex(i);
if(a.getIndex(i) == R.styleable.Follow_target){
targetId = a.getResourceId(attr, -1);
a.recycle();
}@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
child.setY(dependency.getY()+dependency.getHeight());
}@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency.getId() == targetId;
public class FollowBehavior extends CoordinatorLayout.Behavior {private int targetId;public FollowBehavior(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Follow);for (int i = 0; i & a.getIndexCount(); i++) {int attr = a.getIndex(i);if(a.getIndex(i) == R.styleable.Follow_target){targetId = a.getResourceId(attr, -1);}}a.recycle();}@Overridepublic boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { child.setY(dependency.getY()+dependency.getHeight());}@Overridepublic boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {return dependency.getId() == targetId;}}xml中:
&android.support.design.widget.CoordinatorLayoutxmlns:android=&http://schemas.android.com/apk/res/android&
xmlns:app=&http://schemas.android.com/apk/res-auto&
xmlns:tools=&http://schemas.android.com/tools&
android:orientation=&vertical&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:fitsSystemWindows=&true&
tools:context=&.MainActivity&&&View
android:id=&@+id/first&
android:layout_width=&match_parent&
android:layout_height=&128dp&
android:background=&@android:color/holo_blue_light&/&&View
android:id=&@+id/second&
android:layout_width=&match_parent&
android:layout_height=&128dp&
app:layout_behavior=&.FollowBehavior&
app:target=&@id/first&
android:background=&@android:color/holo_green_light&/&
&/android.support.design.widget.CoordinatorLayout&
&android.support.design.widget.CoordinatorLayoutxmlns:android=&http://schemas.android.com/apk/res/android&xmlns:app=&http://schemas.android.com/apk/res-auto&xmlns:tools=&http://schemas.android.com/tools&android:orientation=&vertical&android:layout_width=&match_parent&android:layout_height=&match_parent&android:fitsSystemWindows=&true&tools:context=&.MainActivity&&&Viewandroid:id=&@+id/first&android:layout_width=&match_parent&android:layout_height=&128dp&android:background=&@android:color/holo_blue_light&/&&Viewandroid:id=&@+id/second&android:layout_width=&match_parent&android:layout_height=&128dp&app:layout_behavior=&.FollowBehavior&app:target=&@id/first&android:background=&@android:color/holo_green_light&/&&/android.support.design.widget.CoordinatorLayout&
效果是不管first怎么移动。second都会在他下面。
01.gif滑动
Behavior最大的用处在于对滑动事件的处理。就像CollapsingToolbarLayout的那个酷炫效果一样。
主要是这3个方法,所依赖对象的滑动事件都将通知进来:
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
//这里返回true,才会接受到后续滑动事件。
}@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
//进行滑动事件处理
}@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY, boolean consumed) {
//当进行快速滑动
return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
@Overridepublic boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {//这里返回true,才会接受到后续滑动事件。}@Overridepublic void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {//进行滑动事件处理}@Overridepublic boolean onNestedFling(CoordinatorLayout coordinatorLayout, View child, View target, float velocityX, float velocityY, boolean consumed) {//当进行快速滑动return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);}注意被依赖的View只有实现了NestedScrollingChild接口的才可以将事件传递给CoordinatorLayout。
但注意这个滑动事件是对于CoordinatorLayout的。所以只要CoordinatorLayout有NestedScrollingChild就会滑动,他滑动就会触发这几个回调。无论你是否依赖了那个View。
下面就是一个简单的View跟随ScrollView滑入滑出屏幕的例子。可以是Toolbar或其他任何View。
public class ScrollToTopBehavior extends CoordinatorLayout.Behavior&View&{
int offsetTotal = 0;
boolean scrolling =public ScrollToTopBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
}@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
offset(child, dyConsumed);
}public void offset(View child,int dy){
int old = offsetT
int top = offsetTotal -
top = Math.max(top, -child.getHeight());
top = Math.min(top, 0);
offsetTotal =
if (old == offsetTotal){
scrolling =
int delta = offsetTotal-
child.offsetTopAndBottom(delta);
scrolling =
public class ScrollToTopBehavior extends CoordinatorLayout.Behavior&View&{int offsetTotal = 0;boolean scrolling =public ScrollToTopBehavior(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {}@Overridepublic void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {offset(child, dyConsumed);}public void offset(View child,int dy){int old = offsetTint top = offsetTotal -top = Math.max(top, -child.getHeight());top = Math.min(top, 0);offsetTotal =if (old == offsetTotal){scrolling =}int delta = offsetTotal-child.offsetTopAndBottom(delta);scrolling =}}
&android.support.design.widget.CoordinatorLayout
xmlns:android=&http://schemas.android.com/apk/res/android&
xmlns:app=&http://schemas.android.com/apk/res-auto&
xmlns:tools=&http://schemas.android.com/tools&
android:orientation=&vertical&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:fitsSystemWindows=&false&
tools:context=&.MainActivity&&&android.support.v4.widget.NestedScrollView
android:id=&@+id/second&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_marginTop=&128dp&
android:text=&A/nB/nC/nD/nE/nF/nG/nH/nI/nJ/nK/nL/nM/nN/nO/nP/nQ/nR/nS/nT/nU/nV/nW/nX/nY/nZ&
android:background=&@android:color/holo_red_light&/&
&/LinearLayout&
&/android.support.v4.widget.NestedScrollView&&View
android:id=&@+id/first&
android:layout_width=&match_parent&
android:layout_height=&128dp&
app:layout_behavior=&.ScrollToTopBehavior&
android:background=&@android:color/holo_blue_light&/&&/android.support.design.widget.CoordinatorLayout&
&android.support.design.widget.CoordinatorLayoutxmlns:android=&http://schemas.android.com/apk/res/android&xmlns:app=&http://schemas.android.com/apk/res-auto&xmlns:tools=&http://schemas.android.com/tools&android:orientation=&vertical&android:layout_width=&match_parent&android:layout_height=&match_parent&android:fitsSystemWindows=&false&tools:context=&.MainActivity&&&android.support.v4.widget.NestedScrollViewandroid:id=&@+id/second&android:layout_width=&match_parent&android:layout_height=&match_parent&&&LinearLayoutandroid:layout_width=&match_parent&android:layout_height=&wrap_content&&&TextViewandroid:layout_width=&match_parent&android:layout_height=&wrap_content&android:layout_marginTop=&128dp&style=&@style/TextAppearance.AppCompat.Display3&android:text=&A/nB/nC/nD/nE/nF/nG/nH/nI/nJ/nK/nL/nM/nN/nO/nP/nQ/nR/nS/nT/nU/nV/nW/nX/nY/nZ&android:background=&@android:color/holo_red_light&/&&/LinearLayout&&/android.support.v4.widget.NestedScrollView&&Viewandroid:id=&@+id/first&android:layout_width=&match_parent&android:layout_height=&128dp&app:layout_behavior=&.ScrollToTopBehavior&android:background=&@android:color/holo_blue_light&/&&/android.support.design.widget.CoordinatorLayout&当NestedScrollView滑动的时候,first也能跟着滑动。toolbar和fab的上滑隐藏都可以这样实现。
02.gif事件处理
这2个回调与View中的事件分发是一样的。所有Behavior能在子View之前收到CoordinatorLayout的所有触摸事件。可以进行拦截,如果拦截事件将不会流经子View。因为这2个方法都是在CoordinatorLayout的 回调中
public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
return super.onInterceptTouchEvent(parent, child, ev);
}@Override
public boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {
return super.onTouchEvent(parent, child, ev);
@Overridepublic boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {return super.onInterceptTouchEvent(parent, child, ev);}@Overridepublic boolean onTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {return super.onTouchEvent(parent, child, ev);}
AppBarLayout的收缩原理分析
示例中给可滑动View设的Behavior是@string/appbar_scrolling_view_behavior(android.support.design.widget.AppBarLayout$ScrollingViewBehavior)。
ScrollingViewBehavior的源码不多,看得出唯一的作用是把自己放到AppBarLayout的下面…(不能理解为什么叫ScrollingViewBehavior)所有View都能使用这个Behavior。
AppBarLayout自带一个Behivior。直接在源码里注解声明的。这个Behivior也只能用于AppBarLayout。作用是让他根据CoordinatorLayout上的滚动手势进行一些效果(比如收缩)。与ScrollingViewBehavior是无关的,加不加ScrollingViewBehavior不影响收缩。
只不过只有某些可滑动View才会把滑动事件响应给CoordinatorLayout才能继而响应给AppBarLayout。
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
为您提供0门槛上云实践机会
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
关于 CoordinatorLayout 与 Behavior 的一点分析相关信息,包括
的信息,所有关于 CoordinatorLayout 与 Behavior 的一点分析相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
服务与支持
账号与支持
关注阿里云
International

我要回帖

更多关于 coordinatorlayout坑 的文章

 

随机推荐