本博文专门解说和CoordinatorLayout相关的知识点,这也是Design Support Library中最重要与最难的部分。


概览

CoordinatorLayout实现了多种Material Design中提到的滚动效果。眼下这个框架提供了几种不用写动画代码就能工作的方法,这些效果包含:

让浮动操作按钮上下滑动,为Snackbar留出空间

扩展或者缩小Toolbar或者头部,让主内容区域有很多其它的空间。

控制哪个view应该扩展还是收缩。以及其显示大小比例。包含视差滚动效果parallax scrolling effects动画。


Code Samples

官方为我们提供了一个美丽的demo ,使用了 CoordinatorLayout 和其它的 design support library特性.

详情请查看github

效果图

Setup

Make sure to follow the Design Support Library instructions first.

Floating Action Buttons and Snackbars

The CoordinatorLayout can be used to create floating effects using the layout_anchor and layout_gravity attributes. See the Floating Action Buttons guide for more information.

When a Snackbar is rendered, it normally appears at the bottom of the visible screen. To make room, the floating action button must be moved up to provide space.

So long as the CoordinatorLayout is used as the primary layout, this animation effect will occur for you automatically. The floating action button has a default behavior that detects Snackbar views being added and animates the button above the height of the Snackbar.

Code

activity_fab__snackar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView
android:id="@+id/rvToDoList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/gur_project_10"
app:layout_anchor="@id/rvToDoList"
app:layout_anchorGravity="bottom|right|end"
app:layout_behavior="demo.turing.com.materialdesignwidget.floatingActionButton.ScrollAwareFABBehavior"/> </android.support.design.widget.CoordinatorLayout>

Fab_SnackarAct.java

public class Fab_SnackarAct extends AppCompatActivity {

    private RecyclerView recyclerView;
private FloatingActionButton floatingActionButton;
private CoordinatorLayout coordinatorLayout ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fab__snackar); coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content); floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingActionButton); recyclerView = (RecyclerView) findViewById(R.id.rvToDoList); // 线性布局
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 设置适配器,填充数据
recyclerView.setAdapter(new NormalRecyclerViewAdapter(this)); floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view,"HELLO",Snackbar.LENGTH_SHORT).show();
}
});
}
}

关键点

  • 根布局必须为CoordinatorLayout
  • FloatingActionButton设置 app:layout_anchor和app:layout_anchorGravity属性
  • app:layout_behavior这个属性也能够不加也能实现点击floatingActionButton弹出Snackbar。fab自己主动上移的效果,app:layout_behavior的为自己定义的效果,当下滑时。fab消失,上滑时fab显示,详情请查看本人博客Floating Action Button-Android M新控件

执行图


Expanding and Collapsing Toolbars(Toolbar的扩展与收缩)

  • The first step is to make sure you are not using the deprecated ActionBar. Make sure to follow the Using the ToolBar as ActionBar guide.
  • Also make sure that the CoordinatorLayout is the main layout container.
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.CoordinatorLayout>

Responding to Scroll Events

Next, we must make the Toolbar responsive to scroll events using a container layout called AppBarLayout:

接下来。我们必须使用一个容器布局:AppBarLayout来让Toolbar响应滚动事件。

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout>

Note: AppBarLayout currently expects to be the first child nested within a CoordinatorLayout according to the official Google docs.

注意:依据官方的谷歌文档。AppBarLayout眼下必须是第一个嵌套在CoordinatorLayout里面的子view。

Next, we need to define an association between the AppBarLayout and the View that will be scrolled. Add an app:layout_behavior to a RecyclerView or any other View capable of nested scrolling such as NestedScrollView

The support library contains a special string resource @string/appbar_scrolling_view_behavior that maps to AppBarLayout.ScrollingViewBehavior, which is used to notify the AppBarLayout when scroll events occur on this particular view. The behavior must be established on the view that triggers the event.

然后。我们须要定义AppBarLayout与滚动视图之间的联系。在RecyclerView或者随意支持嵌套滚动的view比方NestedScrollView上加入app:layout_behavior。support library包含了一个特殊的字符串资源@string/appbar_scrolling_view_behavior,它和AppBarLayout.ScrollingViewBehavior相匹配。用来通知AppBarLayout 这个特殊的view何时发生了滚动事件,这个behavior须要设置在触发事件(滚动)的view之上。

<android.support.v7.widget.RecyclerView
android:id="@+id/rvToDoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

当CoordinatorLayout发现RecyclerView中定义了这个属性,它会搜索自己所包含的其它view。看看是否有view与这个behavior相关联。AppBarLayout.ScrollingViewBehavior描写叙述了RecyclerView与AppBarLayout之间的依赖关系。RecyclerView的随意滚动事件都将触发AppBarLayout或者AppBarLayout里面view的改变。

AppBarLayout里面定义的view仅仅要设置了app:layout_scrollFlags属性,就能够在RecyclerView滚动事件发生的时候被触发:

 <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout>

app:layout_scrollFlags属性里面必须至少启用scroll这个flag,这样这个view才会滚动出屏幕。否则它将一直固定在顶部。能够使用的其它flag有:

  • enterAlways: 一旦向上滚动这个view就可见。

Normally, the Toolbar only appears when the list is scrolled to the top as shown below:

  • enterAlwaysCollapsed: 顾名思义,这个flag定义的是何时进入(已经消失之后何时再次显示)。如果你定义了一个最小高度(minHeight)同一时候enterAlways也定义了。那么view将在到达这个最小高度的时候開始显示,而且从这个时候開始慢慢展开,当滚动到顶部的时候展开完。

  • exitUntilCollapsed: 相同顾名思义,这个flag时定义何时退出。当你定义了一个minHeight,这个view将在滚动到达这个最小高度的时候消失。

  • snap:Using this option will determine what to do when a view only has been partially reduced. If scrolling ends and the view size has been reduced to less than 50% of its original, then this view to return to its original size. If the size is greater than 50% of its sized, it will disappear completely.

记住,要把带有scroll flag的view放在前面,这样收回的view才干让正常退出,而固定的view继续留在顶部。

此时,你应该注意到我们的Toolbar能够响应滚动事件了。


Creating Collapsing Effects(制造折叠效果)

如果想制造toolbar的折叠效果。我们必须把Toolbar放在CollapsingToolbarLayout中:

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="? attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="? attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar> </android.support.design.widget.CollapsingToolbarLayout>

如今效果就成了:

通常,我们我们都是设置Toolbar的title。而如今。我们须要把title设置在CollapsingToolBarLayout上,而不是Toolbar。

CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Title");

Note that when using CollapsingToolbarLayout, the status bar should be made translucent (API 19) or transparent (API 21) as shown in this file. In particular, the following styles should be set in res/values-xx/styles.xml as illustrated:

<!-- res/values-v19/styles.xml -->
<style name="AppTheme" parent="Base.AppTheme">
<item name="android:windowTranslucentStatus">true</item>
</style> <!-- res/values-v21/styles.xml -->
<style name="AppTheme" parent="Base.AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>

Creating Parallax Animations(制造视差效果)

CollapsingToolbarLayout还能让我们做出更高级的动画,比方在里面放一个ImageView,然后在它折叠的时候渐渐淡出。同一时候在用户滚动的时候title的高度也会随着改变。

为了制造出这样的效果。我们加入一个定义了app:layout_collapseMode=”parallax” 属性的ImageView。

 <android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="? attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>
<ImageView
android:src="@drawable/cheese_1"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:minHeight="100dp"/> </android.support.design.widget.CollapsingToolbarLayout>

Custom Behaviors (自己定义Behavior)

CoordinatorLayout 与浮动操作按钮中我们讨论了一个自己定义behavior的样例。

译文http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0718/3197.html

CoordinatorLayout的工作原理是搜索定义了CoordinatorLayout Behavior 的子view,无论是通过在xml中使用app:layout_behavior标签还是通过在代码中对view类使用@DefaultBehavior修饰符来加入注解。当滚动发生的时候,CoordinatorLayout会尝试触发那些声明了依赖的子view。

要自己定义CoordinatorLayout Behavior,你须要实现layoutDependsOn() 和onDependentViewChanged()两个方法。比方AppBarLayout.Behavior 就定义了这两个关键方法。

这个behavior用于当滚动发生的时候让AppBarLayout发生改变。

public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof AppBarLayout;
} public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
// check the behavior triggered
android.support.design.widget.CoordinatorLayout.Behavior behavior = ((android.support.design.widget.CoordinatorLayout.LayoutParams)dependency.getLayoutParams()).getBehavior();
if(behavior instanceof AppBarLayout.Behavior) {
// do stuff here
}
}

英文原文:

https://guides.codepath.com/android/Handling-Scrolls-with-CoordinatorLayout

CoordinatorLayout与滚动的处理的更多相关文章

  1. CoordinatorLayout使用全解析

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012124438/article/details/56701641 CoordinatorLayo ...

  2. Android Tips – 填坑手册

    出于: androidChina   http://www.androidchina.net/3595.html 学习 Android 至今,大大小小的坑没少踩,庆幸的是,在强大的搜索引擎与无私奉献的 ...

  3. Android 5.0/5.1开发问题专贴

    注:非5.0特定的开发问题,可以在这个帖子里查:Android开发问题汇总. 1.官方提供的例子android-support-v7-appcompat编译时提示android:actionModeS ...

  4. 总结一下现在关于Design Support Library的几个博客

    原文转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0713/3179.html 关于Design Support Libr ...

  5. android 开发从入门到精通

    Android-Tips This is an awesome list of tips for android. If you are a beginner, this list will be t ...

  6. ot

    https://blog.csdn.net/notice520/article/details/8135600 | android中的跨进程通信的实现(一)——远程调用过程和aidl - CSDN博客 ...

  7. Diycode开源项目 MainActivity分析

    1.分析MainActivity整体结构 1.1.首先看一下这个界面的整体效果. 1.2.活动源代码如下 /* * Copyright 2017 GcsSloop * * Licensed under ...

  8. Android CoordinatorLayout + AppBarLayout(向上滚动隐藏指定的View)

    在新的Android Support Library里面,新增了CoordinatorLayout, AppBarLayout等. 实现的效果: 向下滚动RecylerView,Tab会被隐藏,向上滚 ...

  9. Android Material Design:基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出

    activity_main.xml: <android.support.design.widget.CoordinatorLayout xmlns:android="http://sc ...

随机推荐

  1. Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”

    今天老板让我验证一下技术可行性,记录下来. 需求 :定位手机的位置并在百度地图上显示,得到位置后使用前置摄像头进行抓拍 拿到这个需求后,对于摄像头的使用不太熟悉,于是我先做了定位手机并在百度地图上显示 ...

  2. 深入理解ES6之—set与map

    Set是无重复值的有序列表.Set会自动移除重复的值,因此你可以使用它来过滤数组中重复的值并返回结果. Map是有序的键值对,其中的键允许是任何类型. Set和Map是es6新增的两个数据集合. Se ...

  3. IpHelper根据客户端IP进行网站分流

    public class IpHelper    {        // 核心方法:IP搜索       /// <summary>        /// 查找IP所属地区,确保web.c ...

  4. 新建Vue项目

    新建Vue项目 1.安装Vue命令行工具,webpack和git,使用淘宝镜像CNPM.参考http://www.imooc.com/video/12299 2.打开命令行,进入到要存放的目录,win ...

  5. JS小游戏:贪吃蛇(附源码)

    javascript小游戏:贪吃蛇 此小游戏采用的是面向对象的思想,将蛇,食物,和游戏引擎分为3个对象来写的. 为方便下载,我把js写在了html中, 源码中暂时没有注释,等有空我在添加点注释吧. 游 ...

  6. python调用c代码

    Linux环境下使用python调用C的printf例子: #!/usr/bin/env python2.7 #-*- coding:utf-8 -*- from ctypes import * de ...

  7. MySQL原理相关

    1.索引 http://blog.codinglabs.org/articles/theory-of-mysql-index.html

  8. JSON Web Tokens(JWT)

    现在API越来越流行,如何安全保护这些API? JSON Web Tokens(JWT)能提供基于JSON格式的安全认证.它有以下特点: JWT是跨不同语言的,JWT可以在 .NET, Python, ...

  9. 【Java框架型项目从入门到装逼】第一节 - Spring框架 IOC的丧心病狂解说

    大家好,好久不见,今天我们来一起学习一下关于Spring框架的IOC技术. 控制反转--Spring通过一种称作控制反转(IoC)的技术促进了松耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的 ...

  10. Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法

    找到Help Viewer程序的位置(可以使用Everything工具搜索microsoft help找到),发送到桌面快捷方式,打开此快捷方式的属性,在目标的最后添加下面那行字,然后即可通过快捷方式 ...