CoordinatorLayout与AppBarLayout的配合使用,在之前的文章中我们也经常使用,主要是专门用来打造各种炫酷的效果。

有童鞋看了之前的文章反馈对AppBarLayout中的scrollFlags属性的设置不是很明白,这篇文章我们具体来讲讲这个属性的用法效果。

我们先简单了解一下AppBarLayout:

AppBarLayout继承自LinearLayout,布局方向为垂直方向。所以你可以把它当成垂直布局的LinearLayout来使用。AppBarLayout是在LinearLayou上加了一些材料设计的概念,它可以让你定制当某个可滚动View的滚动手势发生变化时,其内部的子View实现何种动作。

这里说得其内部的子View实现任何动作,就是可以通过scrollFlags属性进行设置达到想要的效果。

那么app:layout_scrollFlags可以设置哪些动作呢?

下面我们通过XML布局文件代码和对应的效果图进行解析:

1、scroll

子View会跟随滚动事件一起发生移动而滚出或滚进屏幕。注意两点:第一点,如果使用了其他值,必定要使用这个值才能起作用;第二点:如果在这个子View前面的任何其他子View没有设置这个值,那么这个子View的设置将失去作用。

布局文件:


  1. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:fitsSystemWindows="true">
  6. <android.support.design.widget.AppBarLayout
  7. android:id="@+id/appbar"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  11. <android.support.v7.widget.Toolbar
  12. android:id="@+id/tb_toolbar"
  13. android:layout_width="match_parent"
  14. android:layout_height="50dp"
  15. android:background="@color/colorPrimary"
  16. app:layout_scrollFlags="scroll"
  17. app:title="@string/app_name" />
  18. </android.support.design.widget.AppBarLayout>
  19. <android.support.v7.widget.RecyclerView
  20. android:id="@+id/recyclerView"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  24. </android.support.design.widget.CoordinatorLayout>

对应效果图:

2、enterAlways

和scroll相比较,其实就是向下滚动时优先级问题,scroll首先滑动的是列表,列表的数据全部滚动完毕,才开始toolbar滑动。而scroll | enterAlways首先滑动的是toolbar ,然后再去滑动其他的view。只是优先级先后的问题。

布局文件:代码类型,只是改变属性值,这里就不赘述了

  1. ......................
  2. <android.support.v7.widget.Toolbar
  3. android:id="@+id/tb_toolbar"
  4. android:layout_width="match_parent"
  5. android:layout_height="50dp"
  6. android:background="@color/colorPrimary"
  7. app:layout_scrollFlags="scroll|enterAlways"
  8. app:title="@string/app_name" />
  9. ......................

对应效果图:

3、enterAlwaysCollapsed

enterAlwaysCollapsed是enterAlways的附加标志,这里涉及到子View的高度和最小高度,向下滚动时,子View先向下滚动最小高度值,然后Scrolling View开始滚动,到达边界时,子View再向下滚动,直至显示完全。

布局文件:代码类型,只是改变属性值,这里就不赘述了


  1. ............................
  2. <android.support.v7.widget.Toolbar
  3. android:id="@+id/tb_toolbar"
  4. android:layout_width="match_parent"
  5. android:layout_height="200dp"
  6. android:minHeight="50dp"
  7. android:background="@color/colorPrimary"
  8. app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
  9. app:title="@string/app_name" />
  10. .............................

对应效果图:

4、exitUntilCollapsed

这里也涉及到最小高度。发生向上滚动事件时,子View向上滚动直至最小高度,然后Scrolling View开始滚动。也就是,子View不会完全退出屏幕。

布局文件:代码类型,只是改变属性值,这里就不赘述了


  1. ...................................
  2. <android.support.v7.widget.Toolbar
  3. android:id="@+id/tb_toolbar"
  4. android:layout_width="match_parent"
  5. android:layout_height="200dp"
  6. android:minHeight="50dp"
  7. android:background="@color/colorPrimary"
  8. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  9. app:title="@string/app_name" />
  10. ....................................

对应效果图:



5、snap

子View滚动比例的吸附效果。也就是说子View不会存在局部显示的情况,滚动到子View的部分高度,当我们松开手指时,子View要么向上全部滚出屏幕,要么向下全部滚进屏幕。

布局文件:代码类型,只是改变属性值,这里就不赘述了

  1. ......................
  2. <android.support.v7.widget.Toolbar
  3. android:id="@+id/tb_toolbar"
  4. android:layout_width="match_parent"
  5. android:layout_height="200dp"
  6. android:background="@color/colorPrimary"
  7. app:layout_scrollFlags="scroll|snap"
  8. app:title="@string/app_name" />
  9. ......................

对应效果图:



6、snapMargins

snapMargins是必须配合snap一起使用的额外的flag。如果设置的话,这个View将会被snap到它的顶部外边距和它的底部外边距的位置,而不是这个View自身的上下边缘。

布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:fitsSystemWindows="true">
  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:background="@color/colorPrimary"
  12. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
  13. <android.support.v7.widget.Toolbar
  14. android:id="@+id/tb_toolbar"
  15. android:layout_width="match_parent"
  16. android:layout_height="100dp"
  17. android:layout_marginStart="10dp"
  18. android:layout_marginTop="200dp"
  19. android:layout_marginEnd="10dp"
  20. android:layout_marginBottom="10dp"
  21. android:background="@color/colorAccent"
  22. app:layout_scrollFlags="scroll|snap|snapMargins"
  23. app:title="@string/app_name" />
  24. </android.support.design.widget.AppBarLayout>
  25. <android.support.v7.widget.RecyclerView
  26. android:id="@+id/recyclerView"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  30. </android.support.design.widget.CoordinatorLayout>

对应的效果图:



可以看到Margin生效了,滑动必须超过Toolbar的高度以及上下Margin就会继续滑动,否则就恢复。

上面的内容就介绍完了,代码基本都在文章里,就不放demo了。

到这里就结束啦!

Android | 玩转AppBarLayout,设置scrollFlags滑动属性详解的更多相关文章

  1. Android textAppearance的属性设置及TextView属性详解

    textAppearance的属性设置 android:textAppearance="?android:attr/textAppearanceSmall" android:tex ...

  2. android中布局文件中 layout_weight 的属性详解

    在不同的情况下,layout_weight属性作用是不同的.主要有两种属性: 1.当布局中的控件的尺寸(宽和高)都有指定时,它所表示的该控件在父容器中的比重,及它在父容器中所占的比例,数值越大,比重越 ...

  3. Android AVD创建及设置中各参数详解

    设置AVD时有些参数比较模糊,特地找了篇文章,大家参考下! 本文根据如下的模拟器安装做一些解释: Name:自定义虚拟的名称,不能有空格或者其他非法字符,否则不能创建,即Creat AVD不能高亮点击 ...

  4. Android中的windowSoftInputMode属性详解

    这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下     在前面的一篇文章中 ...

  5. android:exported 属性详解

    属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...

  6. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

  7. Android TextView和EditText属性详解

    TextView属性详解: autoLink设置 是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web /email/phone/map/all) ...

  8. Android笔记-2-TextView的属性详解

    [Android 基础]TextView的属性详解 android:autoLink :设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web / ...

  9. Android开发–Intent-filter属性详解

    Android开发–Intent-filter属性详解 2011年05月09日 ⁄ Andriod ⁄ 暂无评论 ⁄ 被围观 1,396 views+ 如果一个 Intent 请求在一片数据上执行一个 ...

随机推荐

  1. 痞子衡嵌入式:超级下载算法(RT-UFL)开发笔记(3) - 统一FlexSPI驱动访问

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是超级下载算法开发笔记(3)之统一FlexSPI驱动访问. 文接上篇 <超级下载算法(RT-UFL)开发笔记(2) - 识别当前i. ...

  2. 使用MCSManager搭建Minecraft服务器

    目录 一.准备工作 1.MCSManager Windows环境下安装 Linux安装 2.Minecraft服务端 3.Java 二.配置 1.登录面板 2.上传服务端 3.服务端的配置 三.开启服 ...

  3. Ubuntu 18.04 + pip3 install virtualenvwrapper 找不到virtualenvwrapper.sh

    Reference Ubuntu 18.04 只自带python3.6.5, 因此不想装python2了, 但通过apt install 装virtualenvwrapper时发现必须得装python ...

  4. certbot 获取数字证书失效问题

    title: certbot 获取数字证书失效问题 author: Narule date: 2021-02-18 10:45:00 +0800 categories: [Technology^技术, ...

  5. 如何优雅的阅读 GitHub 上开源 js 框架和库的源码

    如何优雅的阅读 GitHub 上开源 js 框架和库的源码 step 先总后分,即先了解一下啊框架的大体架构,又一个全局的认识,在选择某些和感兴趣的部分,仔细阅读,各个击破: 带着问题阅读,用到了什么 ...

  6. Programming Interview Questions Websites All In One

    Programming Interview Questions Websites All In One 编程面试刷题网站 http://highscalability.com/ https://tri ...

  7. 使用 js 实现十大排序算法: 插入排序

    使用 js 实现十大排序算法: 插入排序 插入排序 // 双重循环 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  8. flutter 插件调用callback函数

    dart plugin class TestLib { static MethodChannel _channel = const MethodChannel('test_lib') ..setMet ...

  9. nasm astrrchr函数 x86

    xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...

  10. 打造NGK生态星空计划,高倍币VAST即将震撼上线!

    援引华盛顿邮报.彭博社.路透社以及CNN等知名媒体的报道,NGK官方近日宣布,为了完善NGK生态星空计划,NGK官方近日即将推出SPC的子币VAST,以鼓励更多的生态建设者参与. NGK官方相关负责人 ...