前言
- Android支持库版本 com.android.support:design-26.1.0
- 基于源码注释来学习AppBarLayout的基本使用
目录
- 1 AppBarLayout?
- 2.基本使用
- 3.常用属性
- 4.基础Demo
- 5.注意事项
1.AppBarLayout?
AppBarLayout is a vertical LinearLayout which implements many of the features of
material designs app bar concept, namely scrolling gestures.
-
AppBarLayout是一个垂直的LinearLayout
- AppBarLayout是继承于LinearLayout,并且在初始化的时候设置内部子元素为垂直分布
public AppBarLayout(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(VERTICAL);//设置内部子元素为垂直分布 ... }
- 重写了父类的方法,保证
@Override public void setOrientation(int orientation) { if (orientation != VERTICAL) {//这里限制了只能是垂直分布 throw new IllegalArgumentException("AppBarLayout is always vertical and does" + " not support horizontal orientation"); } super.setOrientation(orientation); }
- AppBarLayout是继承于LinearLayout,并且在初始化的时候设置内部子元素为垂直分布
- 实现了许多Material Designs设计概念的AppBar,即滚动手势
2.基本使用
Children should provide their desired scrolling behavior through `setScrollFlags(int)`
and the associated layout xml attribute:`app:layout_scrollFlags`.
- 它的子View,需要通过setScrollFlags(int)来提供它们想要的滚动行为
或者在关联的XML中设置app:layout_scrollFlags属性
AppBarLayout also requires a separate scrolling sibling in order to know when to scroll.
The binding is done through the ScrollingViewBehavior behavior class,
meaning that you should set your scrolling view's behavior to be an instance of ScrollingViewBehavior.
A string resource containing the full class name is available.
-
AppBarLayout还需要一个单独的滚动同胞,以便知道何时滚动。
- 意思就是还需要一个支持滚动的View来配合使用,比如NestedScrollView
-
绑定是通过ScrollingViewBehavior类完成的,
这意味着您应该将滚动视图的行为设置为ScrollingViewBehavior的实例。
可用一个包含完整类名的字符串资源。<android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior">//同AppBarLayout关联
3.相关属性和方法
attr | 作用 |
---|---|
app:layout_scrollFlags | 子View设置滚动方式 |
3.1 layout_scrollFlags
-
SCROLL_FLAG_SCROLL(支持滚动)
The view will be scroll in direct relation to scroll events. This flag needs to be set for any of the other flags to take effect. If any sibling views before this one do not have this flag, then this value has no effect.
- 设置了这个属性,View就会关联到滚动事件。
如果要使用其他的标签,必须先设置这个标签
如果在使用其他标签之前,没设置该标签,则其他值无效
- 设置了这个属性,View就会关联到滚动事件。
-
SCROLL_FLAG_EXIT_UNTIL_COLLAPSED(折叠效果)
When exiting (scrolling off screen) the view will be scrolled until it is * 'collapsed'. The collapsed height is defined by the view's minimum height.
- 当从屏幕上停止滚动的时,View将被滚动直到collapsed(折叠),
collapsed高度由视图的最小高度定义。
- 当从屏幕上停止滚动的时,View将被滚动直到collapsed(折叠),
-
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. This is commonly referred to as the 'quick return' pattern.
- 当进入(屏幕上滚动)时,视图将向下滚动
滚动事件,不管滚动视图是否也滚动。这
通常被称为“快速返回”模式。
- 当进入(屏幕上滚动)时,视图将向下滚动
-
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. Once the scrolling view has reached the end of it's scroll range, the remainder of this view will be scrolled into view. The collapsed height is defined by the view's minimum height.
- “enterAlways”的附加标志,它修改返回的视图
只有最初滚动回它的折叠高度。一旦滚动视图
到达滚动范围的末尾,这个视图的其余部分将被滚动。
折叠高度由视图的最小高度定义。
- “enterAlways”的附加标志,它修改返回的视图
-
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. For example, if the view only has it's bottom 25% displayed, it will be scrolled off screen completely. Conversely, if it's bottom 75% is visible then it will be scrolled fully into view.
- 在滚动结束时,如果视图只是部分可见,那么它将被关闭
滚动到最近的边缘。例如,如果视图只有底部的25%
显示,它将被完全滚出屏幕。相反,如果是底部的75%
是可见的,然后它将被完全滚动到视图中。
- 在滚动结束时,如果视图只是部分可见,那么它将被关闭
4.基础Demo
- 示例XML
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="动\n次\n打\n次\n"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="?actionBarSize"
app:layout_scrollFlags="scroll"
app:title="Title"/>
<android.support.design.widget.TabLayout
android:id="@+id/tl_demo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
5.注意事项
This view depends heavily on being used as a direct child within a CoordinatorLayout.
If you use AppBarLayout within a different ViewGroup, most of it's functionality will not work.
- AppBarLayout很大程度上依赖于作为CoordinatorLayout的子元素来使用
- 如果您在不同的ViewGroup中使用AppBarLayout,那么它的大部分功能将无法工作。
6.占坑
6.0以上设备录制屏幕有问题之后补上图
网友评论