[关闭]
@xujun94 2016-10-19T05:30:50.000000Z 字数 10572 阅读 2797

使用CoordinatorLayout打造各种炫酷的效果

转载请注明博客地址:http://blog.csdn.net/gdutxiaoxu/article/details/52858598

CoordinatorLayout简介

CoordinatorLayout是在 Google IO/15 大会发布的,遵循Material 风格,包含在 support Library中,结合AppbarLayout, CollapsingToolbarLayout等 可 产生各种炫酷的效果

CoordinatorLayout简介通常用来 干什么

Google官方地址

CoordinatorLayout is intended for two primary use cases:

As a top-level application decor or chrome layout

As a container for a specific interaction with one or more child views

简单来说就是
- 作为最上层的View
- 作为一个 容器与一个或者多个子View进行交互

下面我们一起先来看一下我们实现的效果图

动态图

结合ToolBar

结合ViewPager

ViewPager

结合ViewPager的视觉特差


AppBarLayout

它是继承与LinearLayout的,默认 的 方向 是Vertical

类型 说明
int SCROLL_FLAG_ENTER_ALWAYS When entering (scrolling on screen) the view will scroll on any downwards scroll event, regardless of whether the scrolling view is also scrolling.
int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED An additional flag for 'enterAlways' which modifies the returning view to only initially scroll back to it's collapsed height.
int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED When exiting (scrolling off screen) the view will be scrolled until it is 'collapsed'.
int SCROLL_FLAG_SCROLL The view will be scroll in direct relation to scroll events.
int SCROLL_FLAG_SNAP Upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to it's closest edge.
类型 说明
int SCROLL_FLAG_ENTER_ALWAYS W((entering) / (scrolling on screen))下拉的时候,这个View也会跟着滑出。
int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED 另一种enterAlways,但是只显示折叠后的高度。
int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED ((exiting) / (scrolling off screen))上拉的时候,这个View会跟着滑动直到折叠。
int SCROLL_FLAG_SCROLL 这个View将会响应Scroll事件
int SCROLL_FLAG_SNAP 在Scroll滑动事件结束以前 ,如果这个View部分可见,那么这个View会停在最接近当前View的位置

我们可以通过两种 方法设置这个Flag

  1. setScrollFlags(int)
  1. app:layout_scrollFlags="scroll|enterAlways"

注意事项

AppBarLayout必须作为CoordinatorLayout的直接子View,否则它的大部分功能将不会生效,如layout_scrollFlags等。

首先我们先来看一下我们 效果图一是怎样实现的

代码

  1. <android.support.design.widget.CoordinatorLayout
  2. android:id="@+id/main_content"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <android.support.design.widget.AppBarLayout
  8. android:id="@+id/appbar"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  12. <android.support.v7.widget.Toolbar
  13. android:id="@+id/toolbar"
  14. android:layout_width="match_parent"
  15. android:layout_height="?attr/actionBarSize"
  16. android:background="?attr/colorPrimary"
  17. app:layout_scrollFlags="scroll|enterAlways"
  18. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  19. .
  20. </android.support.design.widget.AppBarLayout>
  21. <android.support.v7.widget.RecyclerView
  22. android:id="@+id/recyclerView"
  23. android:layout_width="match_parent"
  24. android:layout_height="match_parent"
  25. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  26. <android.support.design.widget.FloatingActionButton
  27. android:id="@+id/fab"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="end|bottom"
  31. android:layout_margin="15dp"
  32. android:src="@drawable/add_2"/>
  33. </android.support.design.widget.CoordinatorLayout>

思路 分析

从图中我们可以知道 layout_scrollFlags="scroll|enterAlways,
前面已经说到layout_scrollFlags=scroll的时候,这个View会 跟着 滚动 事件响应,
layout_scrollFlags=“enterAlways”的时候 这个View会响应下拉事件
所以呈现出来的结果应该是我们在上拉的时候toolBar 会隐藏,下拉的时候toolBar会出来

那如果当我们的toolBar 等于 app:layout_scrollFlags="scroll|snap"的时候 ,
layout_scrollFlags=scroll的时候,这个View会 跟着 滚动 事件响应,
layout_scrollFlags=“snap”的时候 在Scroll滑动事件结束以前 ,如果这个View部分可见,那么这个View会停在最接近当前View的位置。
综上呈现的效果如下,代码见ToolBarSampleSnar的布局文件

结合ViewPager

布局代码如下

  1. <android.support.design.widget.CoordinatorLayout
  2. android:id="@+id/main_content"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <android.support.design.widget.AppBarLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="250dp">
  10. <ImageView android:layout_width="match_parent"
  11. android:layout_height="200dp"
  12. android:background="?attr/colorPrimary"
  13. android:scaleType="fitXY"
  14. android:src="@drawable/tangyan"
  15. app:layout_scrollFlags="scroll|enterAlways"/>
  16. <android.support.design.widget.TabLayout
  17. android:id="@+id/tabs"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentBottom="true"
  21. android:background="?attr/colorPrimary"
  22. app:tabIndicatorColor="@color/colorAccent"
  23. app:tabIndicatorHeight="4dp"
  24. app:tabSelectedTextColor="#000"
  25. app:tabTextColor="#fff"/>
  26. </android.support.design.widget.AppBarLayout>
  27. <android.support.v4.view.ViewPager
  28. android:id="@+id/viewpager"
  29. android:layout_width="match_parent"
  30. android:layout_height="match_parent"
  31. app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
  32. <android.support.design.widget.FloatingActionButton
  33. android:id="@+id/fab"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_gravity="end|bottom"
  37. android:layout_margin="15dp"
  38. android:src="@drawable/add_2"/>
  39. </android.support.design.widget.CoordinatorLayout>

思路分析

其实相对于前 一个例子,只是把 摆放RecyclerView 的位置替换成ViewPager而已,为了有页面导航器的效果,再使用 TabLayout而已,而TabLayout 在我们滑动的时候最终会停靠在 最顶部,是因为我们没有设置其layout_scrollFlags,即TabLayout是静态的

运行以后,即可看到以下的结果

ViewPager

下面我们一起来看一下 TabLayout是怎样结合ViewPager直线 导航器的效果的

代码注释 里面已经解释地很清楚了 ,这里我就不解释了

  1. public class ViewPagerSample extends AppCompatActivity {
  2. ViewPager mViewPager;
  3. List<Fragment> mFragments;
  4. String[] mTitles = new String[]{
  5. "主页", "微博", "相册"
  6. };
  7. private TabLayout mTabLayout;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_third);
  12. // 第一步,初始化ViewPager和TabLayout
  13. mViewPager = (ViewPager) findViewById(R.id.viewpager);
  14. mTabLayout = (TabLayout) findViewById(R.id.tabs);
  15. setupViewPager();
  16. }
  17. private void setupViewPager() {
  18. mFragments = new ArrayList<>();
  19. for (int i = 0; i < mTitles.length; i++) {
  20. ListFragment listFragment = ListFragment.newInstance(mTitles[i]);
  21. mFragments.add(listFragment);
  22. }
  23. // 第二步:为ViewPager设置适配器
  24. BaseFragmentAdapter adapter =
  25. new BaseFragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);
  26. mViewPager.setAdapter(adapter);
  27. // 第三步:将ViewPager与TableLayout 绑定在一起
  28. mTabLayout.setupWithViewPager(mViewPager);
  29. }
  30. }

如果我们想更改Indicator的相关样式,我们可以在布局文件里面使用

  1. <android.support.design.widget.TabLayout
  2. android:id="@+id/tabs"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_alignParentBottom="true"
  6. android:background="?attr/colorPrimary"
  7. app:tabIndicatorColor="@color/colorAccent"
  8. app:tabIndicatorHeight="4dp"
  9. app:tabSelectedTextColor="#000"
  10. app:tabTextColor="#fff"/>

如果你不想使用Google 帮我们 封装好的控件的话,你也可以自己自定义一个控件,你可以参考我的这一篇博客仿网易新闻的顶部导航指示器


在看例子结合ViewPager的视觉特差之前 ,我们需要先了解CollapsingToolbarLayout这个控件

CollapsingToolbarLayout

CollapsingToolbarLayout继承与FrameLayout,官网地址,请自备梯子。

简单来说 ,CollapsingToolbarLayout是工具栏的包装器,它通常作为AppBarLayout的孩子。主要实现以下功能
- Collapsing title(可以折叠 的 标题 )
- Content scrim(内容装饰),当我们滑动的位置 到达一定阀值的时候,内容 装饰将会被显示或者隐藏
- Status bar scrim(状态栏布)
- Parallax scrolling children,滑动的时候孩子呈现视觉特差效果
- Pinned position children,固定位置的 孩子

下面我们一起来看一下几个常量

常量 解释说明
int COLLAPSE_MODE_OFF The view will act as normal with no collapsing behavior.(这个 View将会 呈现正常的结果,不会表现出折叠效果)
int COLLAPSE_MODE_PARALLAX The view will scroll in a parallax fashion. See setParallaxMultiplier(float) to change the multiplier used.(在滑动的时候这个View 会呈现 出 视觉特差效果 )
int COLLAPSE_MODE_PIN The view will pin in place until it reaches the bottom of the CollapsingToolbarLayout.(当这个View到达 CollapsingToolbarLayout的底部的时候,这个View 将会被放置,即代替整个CollapsingToolbarLayout)

我们有两种方法可以设置这个常量,

方法一:在代码中使用这个方法

  1. setCollapseMode(int collapseMode)

方法 二:在布局文件中使用自定义属性

  1. app:layout_collapseMode="pin"

到此 ,CollapsingToolbarLayout的一些重要属性已经讲解完毕,下面我们一起来看一下我们是怎样结合ViewPager实现视差效果的


结合ViewPager的视觉特差

布局代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@android:color/background_light"
  8. android:fitsSystemWindows="true"
  9. >
  10. <android.support.design.widget.AppBarLayout
  11. android:id="@+id/main.appbar"
  12. android:layout_width="match_parent"
  13. android:layout_height="300dp"
  14. android:fitsSystemWindows="true"
  15. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  16. >
  17. <android.support.design.widget.CollapsingToolbarLayout
  18. android:id="@+id/main.collapsing"
  19. android:layout_width="match_parent"
  20. android:layout_height="250dp"
  21. android:fitsSystemWindows="true"
  22. app:contentScrim="?attr/colorPrimary"
  23. app:expandedTitleMarginEnd="64dp"
  24. app:expandedTitleMarginStart="48dp"
  25. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  26. >
  27. <ImageView
  28. android:id="@+id/main.backdrop"
  29. android:layout_width="match_parent"
  30. android:layout_height="match_parent"
  31. android:fitsSystemWindows="true"
  32. android:scaleType="centerCrop"
  33. android:src="@drawable/tangyan"
  34. app:layout_collapseMode="parallax"
  35. />
  36. <android.support.v7.widget.Toolbar
  37. android:id="@+id/toolbar"
  38. android:layout_width="match_parent"
  39. android:layout_height="?attr/actionBarSize"
  40. app:layout_collapseMode="pin"
  41. app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
  42. />
  43. </android.support.design.widget.CollapsingToolbarLayout>
  44. <android.support.design.widget.TabLayout
  45. android:id="@+id/tabs"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_alignParentBottom="true"
  49. android:background="?attr/colorPrimary"
  50. app:tabIndicatorColor="@color/colorAccent"
  51. app:tabIndicatorHeight="4dp"
  52. app:tabSelectedTextColor="#000"
  53. app:tabTextColor="#fff"/>
  54. </android.support.design.widget.AppBarLayout>
  55. <android.support.v4.view.ViewPager
  56. android:id="@+id/viewpager"
  57. android:layout_width="match_parent"
  58. android:layout_height="match_parent"
  59. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  60. </android.support.v4.view.ViewPager>
  61. <android.support.design.widget.FloatingActionButton
  62. android:id="@+id/fab"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:layout_gravity="end|bottom"
  66. android:layout_margin="15dp"
  67. android:src="@drawable/add_2"/>
  68. </android.support.design.widget.CoordinatorLayout>

效果图如下

思路解析

拓展

如果我们仅仅 改变CollapsingToolbarLayout的app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"的时候,其它代码不变,运行以后,我们将可以看到如下效果图


总结

这篇博客主要讲解了CoordinatorLayout,AppBarLayout,CollapsingToolbarLayout的一些相关属性。
- 对于AppBarLayout,我们主要 讲解了这个属性app:layout_scrollFlags,设置不同 的属性我们可以在滚动的时候显示不同 的效果
- 对于CollapsingToolbarLayout,我们主要讲解了app:layout_collapseMode这个属性,设置不同的值,我们可以让其子View呈现不同的 炫酷效果,如parallax和pin等

CoordinatorLayout的相关用法还有很多,有兴趣 了解的请自行阅读: 官方文档地址


题外话

CoordinatorLayout这个控件真的很强大,使用它可以实现各种炫酷的效果,简化了开发者的许多工作,有能力的话可以去研究一下源码 ,看是怎样实现的?

源码下载地址:https://github.com/gdutxiaoxu/CoordinatorLayoutExample.git

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注