AppbarLayout

作者: CQ_TYL | 来源:发表于2018-07-02 14:31 被阅读40次

    AppbarLayout继承自LinearLayout,它就是一个垂直方向的LinearLayout,在LinearLayout的基础上添加了一些材料设计的概念和特性,即滑动手势。它可以让你定制在某个可滑动的View(如:ScrollView ,ListView ,RecyclerView 等)滑动手势发生改变时,内部的子View 该做什么动作。子View应该提供滑动时他们期望的响应的动作Behavior,通过setScrollFlags(int),或者xml 中使用属性:

    app:layout_scrollFlags
    

    注意:AppbarLayout 严重依赖于CoordinatorLayout,必须用于CoordinatorLayout 的直接子View,如果你将AppbarLayout 放在其他的ViewGroup 里面,那么它的这些功能是无效的。

    AppbarLayout 子View 的几种动作

    上面说了 AppbarLayout 可以定制当某个可滑动的View滑动手势改变时内部子View的动作,通过app:layout_scrollFlags来指定,那么现在我们就看一下layout_scrollFlags有哪几种动作。layout_scrollFlags有5种动作,分别是

    scroll,enterAlways,enterAlwaysCollapsed,exitUntilCollapsed,snap
    

    我们来分别看一下这五种动作的含义。

    1. scroll :

    子View 添加layout_scrollFlags属性 的值scroll 时,这个View将会随着可滚动View(如:ScrollView,以下都会用ScrollView 来代替可滚动的View )一起滚动,就好像子View 是属于ScrollView的一部分一样。
    (子view指的不一定是Toolbar)

    <android.support.v7.widget.Toolbar       
                       android:layout_width="match_parent"
                       android:layout_height="?attr/actionBarSize"
                       app:title="AppbarLayout"
                       app:titleTextColor="@color/white"
                       app:layout_scrollFlags="scroll"
                       >
                   </android.support.v7.widget.Toolbar>
    
    2. enterAlways

    子View 添加layout_scrollFlags属性 的值有enterAlways 时, 当ScrollView 向下滑动时,子View 将直接向下滑动,而不管ScrollView 是否在滑动。注意:要与scroll 搭配使用,否者是不能滑动的。
    代码如下:

    <android.support.v7.widget.Toolbar
                       android:layout_width="match_parent"
                       android:layout_height="?attr/actionBarSize"
                       app:title="AppbarLayout"
                       app:titleTextColor="@color/white"
                       app:layout_scrollFlags="scroll|enterAlways"
                       />
    
    3. enterAlwaysCollapsed

    enterAlwaysCollapsed 是对enterAlways 的补充,当ScrollView 向下滑动的时候,滑动View(也就是设置了enterAlwaysCollapsed 的View)下滑至折叠的高度,当ScrollView 到达滑动范围的结束值的时候,滑动View剩下的部分开始滑动。这个折叠的高度是通过View的minimum height (最小高度)指定的。
    补充说明:要配合scroll|enterAlways 一起使用

    <android.support.v7.widget.Toolbar
                       android:layout_width="match_parent"
                       android:layout_height="200dp"
                       android:minHeight="?attr/actionBarSize"
                       app:title="AppbarLayout"
                       android:gravity="bottom"
                       android:layout_marginBottom="25dp"
                       app:titleTextColor="@color/white"
                       app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                       />
    
    4. exitUntilCollapsed

    当ScrollView 滑出屏幕时(也就时向上滑动时),滑动View先响应滑动事件,滑动至折叠高度,也就是通过minimum height 设置的最小高度后,就固定不动了,再把滑动事件交给 scrollview 继续滑动。

    <android.support.v7.widget.Toolbar
                       android:layout_width="match_parent"
                       android:layout_height="200dp"
                       android:minHeight="?attr/actionBarSize"
                       app:title="AppbarLayout"
                       android:gravity="bottom"
                       app:titleTextColor="@color/white"
                       app:layout_scrollFlags="scroll|exitUntilCollapsed"
                       />
    
    5. snap

    意思是:在滚动结束后,如果view只是部分可见,它将滑动到最近的边界。比如,如果view的底部只有25%可见,它将滚动离开屏幕,而如果底部有75%可见,它将滚动到完全显示。

    解释:可能这段话有点难懂,解释一下,就是说,比如在屏幕的顶部有个View ,高度200dp,我向上滑动40%后停止,也就 40% 滑出了屏幕,剩下的60%留在屏幕,那么这个属性就会自动将屏幕外的40% 滑回屏幕,结果的整个View都留在屏幕上,相反,如果我向上将60%的部分滑出屏幕,然后停止滑动,那么这个属性会将剩下的40% 也自动滑出屏幕,结果是整个View都在屏幕之外。这就是上面所说的滑动到最近的边界。

    <android.support.v7.widget.Toolbar
                       android:layout_width="match_parent"
                       android:layout_height="200dp"
                       android:minHeight="?attr/actionBarSize"
                       app:title="AppbarLayout"
                       android:gravity="bottom"
                       app:titleTextColor="@color/white"
                       app:layout_scrollFlags="scroll|snap"
                       />
    

    AppbarLayout 的几个重要方法

    介绍一下AppbarLayout几个常用且重要的方法

    • addOnOffsetChangedListener 当AppbarLayout 的偏移发生改变的时候回调,也就是子View滑动。
    • getTotalScrollRange 返回AppbarLayout 所有子View的滑动范围
    • removeOnOffsetChangedListener移除监听器
    • setExpanded (boolean expanded, boolean animate)**设置AppbarLayout 是展开状态还是折叠状态,animate 参数控制切换到新的状态时是否需要动画
    • setExpanded (boolean expanded)** 设置AppbarLayout 是展开状态还是折叠状态,默认有动画

    完整代码:

    <?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:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <!--app:layout_scrollFlags
            1、scroll: 所有想滚动出屏幕的view都需要设置这个flag, 没有设置这个flag的view将被固定在屏幕顶部。
            例如,TabLayout 没有设置这个值,将会停留在屏幕顶部。
            2、enterAlways: 设置这个flag时,向下的滚动都会导致该view变为可见,启用快速“返回模式”。
            3、enterAlwaysCollapsed: 当你的视图已经设置minHeight属性又使用此标志时,你的视图只能已最小高度进入,
            只有当滚动视图到达顶部时才扩大到完整高度。
            4、exitUntilCollapsed: 滚动退出屏幕,最后折叠在顶端。-->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/Theme.AppCompat.Light" />
    
        </android.support.design.widget.AppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    转自:https://www.jianshu.com/p/ac56f11e7ce1

    相关文章

      网友评论

        本文标题:AppbarLayout

        本文链接:https://www.haomeiwen.com/subject/yhcluftx.html