内容简介:原文首发于微信公众号:jzman-blog,欢迎关注交流!AppBarLayout 是一个垂直方向的 LinearLayout,它实现了许多符合 Material Design 设计规范的状态栏应该具有的功能,比如滚动手势。AppBarLayout 一般直接用作 CoordinatorLayout 的直接子对象,如果 AppBarLayout 在别的布局中使用,其大部分功能会失效,AppBarLayout 需要一个标示才能够知道滚动视图什么时候滚动,这个标示过程是通过绑定 AppBarLayout.Scro
原文首发于微信公众号:jzman-blog,欢迎关注交流!
AppBarLayout 是一个垂直方向的 LinearLayout,它实现了许多符合 Material Design 设计规范的状态栏应该具有的功能,比如滚动手势。
AppBarLayout 一般直接用作 CoordinatorLayout 的直接子对象,如果 AppBarLayout 在别的布局中使用,其大部分功能会失效,AppBarLayout 需要一个标示才能够知道滚动视图什么时候滚动,这个标示过程是通过绑定 AppBarLayout.ScrollingViewBehavior 类完成的,这意味着应该将滚动视图的行为设置为 AppBarLayout.ScrollingViewBehavior的一个实例,这里设置包含完整类名的字符串资源,具体如下:
//设置layout_behavior属性
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- Your scrolling content -->
</android.support.v4.widget.NestedScrollView>
复制代码
AppBarLayout 的子 View 应该设置一个可供滚动的 behavior,可以通过代码和 xml 属性设置,具体如下:
//代码 setScrollFlags(int) //xml attribute app:layout_scrollFlags 复制代码
layout_scrollFlags 属性主要是指定 AppBarLayout 子 View 当滑动手势变化时,AppBarLayout 的子 View 执行什么动作,layout_scrollFlags 属性有 5 个值,分别是:
- scroll
- enterAlways
- enterAlwaysCollapsed
- exitUntilCollapsed
- snap
在介绍这几个属性值之前,这些属性值的效果将以下面布局效果为例,布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.manu.materialdesignsamples.samples.SampleCoordinatorAppBarActivity">
<!--AppBarLayout——>子View设置layout_scrollFlags属性-->
<android.support.design.widget.AppBarLayout
android:id="@+id/ablBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="70dp"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!--NestedScrollView——>设置layout_behavior属性-->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvAppBarData"
android:clipToPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
复制代码
scroll:当设置 layout_scrollFlags 的属性为如下时:
app:layout_scrollFlags="scroll" 复制代码
此时,上滑时先隐藏 AppBarLayout,然后再开始滚动,下滑时直到滚动视图的顶部出现 AppBarLayout 才开始显示,效果如下:
enterAlways:enterAlways 必须配合 scroll 来使用,当设置 layout_scrollFlags 属性为如下时:
app:layout_scrollFlags="scroll|enterAlways" 复制代码
此时,上滑时先隐藏AppBarLayout,然后再开始滚动,下滑时先显示AppBarLayout,然后再开始滚动,效果如下:
enterAlwaysCollapsed:使用 enterAlwaysCollapsed 属性值时,必须配合 scroll 和 enterAlways 来使用,此外还需设置 AppBarLayout 的子 View (这里是 Toolbar)一个最小高度 来提供 enterAlwaysCollapsed 的功能支持,当设置 layout_scrollFlags 的属性为如下时:
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed" 复制代码
此时,上滑时先隐藏AppBarLayout,然后再开始滚动,下滑时 AppBarLayout 先显示子 View 最小高度,然后直到滚动视图的顶部出现 AppBarLayout 全部显示,效果如下:
exitUntilCollapsed:使用 exitUntilCollapsed 属性值时,必须配合 scroll 来使用,此外还需设置 AppBarLayout 的子 View (这里是 Toolbar)一个最小高度 来提供 exitUntilCollapsed 的功能支持,当设置 layout_scrollFlags 的属性为如下时:
app:layout_scrollFlags="scroll|exitUntilCollapsed" 复制代码
此时,上滑时先隐藏 AppBarLayout 至最小高度,然后再开始滚动,下滑时直到滚动视图的顶部出现 AppBarLayout 才开始显示,效果如下:
snap:使用 snap 属性值时,必须配合 scroll 来使用,主要作用是在滚动结束时,如果伸缩的视图只是部分可见,那么它将自动滚动到最近的边缘,当设置 layout_scrollFlags 属性为如下时:
app:layout_scrollFlags="scroll|snap" 复制代码
此时,伸缩的视图(这里是 Toolbar)如果部分可见,那么伸缩的视图将自动滚动到最近的边缘,即要么隐藏、要么显示,效果如下:
关于 layout_scrollFlags 属性就介绍完了,当然上面只是为了说明属性值的效果,还可以结合 CollapsingToolbarLayout 等其他 Material Design 实现酷炫的效果,上面是在布局文件对 layout_scrollFlags 属性的设置,顺便说一下如何在代码中设置 layout_scrollFlags 呢,具体如下:
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) appBarLayout
.getChildAt(0).getLayoutParams();
//上滑时先隐藏AppBarLayout,然后再开始滚动,下滑时先显示AppBarLayout,然后再开始滚动
params.setScrollFlags(
AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL |
AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
//...
复制代码
AppBarLayout 的使用及其重要属性已经介绍完了,实际开发中肯定要复杂的多,希望上面的内容能够对你有所帮助。可以选择关注个人微信公众号:jzman-blog 获取最新更新,一起交流学习!
以上所述就是小编给大家介绍的《Material Design组件之AppBarLayout》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- React 组件模式-有状态组件 x 无状态组件、容器组件 x 展示组件、高阶组件 x 渲染回调(函数作为子组件)
- Serverless 组件开发尝试:全局变量组件和单独部署组件
- angular自定义组件-UI组件篇-switch组件
- React Hooks 源码解析(一):类组件、函数组件、纯组件
- Vue动态组件和异步组件
- Vue 动态组件 & 异步组件原理
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to the Analysis of Algorithms
Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99
This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!