关于android:Android-玩转AppBarLayout设置scrollFlags滑动属性详解

7次阅读

共计 4780 个字符,预计需要花费 12 分钟才能阅读完成。

CoordinatorLayout 与 AppBarLayout 的配合应用,在之前的文章中咱们也常常应用,次要是专门用来打造各种炫酷的成果。

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

咱们先简略理解一下 AppBarLayout:

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

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

那么 app:layout_scrollFlags 能够设置哪些动作呢?

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

1、scroll

子 View 会追随滚动事件一起产生挪动而滚出或滚进屏幕。留神两点:第一点,如果应用了其余值,必然要应用这个值能力起作用;第二点:如果在这个子 View 后面的任何其余子 View 没有设置这个值,那么这个子 View 的设置将失去作用。

布局文件:


<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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll"
            app:title="@string/app_name" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

对应效果图:

2、enterAlways

和 scroll 相比拟,其实就是向下滚动时优先级问题,scroll 首先滑动的是列表,列表的数据全副滚动结束,才开始 toolbar 滑动。而 scroll | enterAlways 首先滑动的是 toolbar,而后再去滑动其余的 view。只是优先级先后的问题。

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

......................
 <android.support.v7.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="@string/app_name" />
 ......................

对应效果图:

3、enterAlwaysCollapsed

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

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


............................
 <android.support.v7.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:minHeight="50dp"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            app:title="@string/app_name" />
.............................

对应效果图:

4、exitUntilCollapsed

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

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


...................................
<android.support.v7.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:minHeight="50dp"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:title="@string/app_name" />
 ....................................

对应效果图:


5、snap

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

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

......................
 <android.support.v7.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/colorPrimary"
            app:layout_scrollFlags="scroll|snap"
            app:title="@string/app_name" />
......................

对应效果图:


6、snapMargins

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

布局文件:

<?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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tb_toolbar"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="200dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:background="@color/colorAccent"
            app:layout_scrollFlags="scroll|snap|snapMargins"
            app:title="@string/app_name" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

对应的效果图:


能够看到 Margin 失效了,滑动必须超过 Toolbar 的高度以及高低 Margin 就会持续滑动,否则就复原。

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

到这里就完结啦!

正文完
 0