美文网首页
AppBarLayout的五种ScrollFlags

AppBarLayout的五种ScrollFlags

作者: 开发小菜鸟 | 来源:发表于2020-01-09 14:08 被阅读0次

ScrollFlags共有五种常量值供AppBarLayoutChild View使用,在xml布局文件中通过app:layout_scrollFlags设置,对应的值为:
scroll
enterAlways
enterAlwaysCollapsed
exitUntilCollapsed
snap
也可以在代码中使用setScrollFlags(int)进行设置,具体使用及效果请看下面内容

scroll:

屏幕向上滑动时AppBarLayout中的Toolbar先被隐藏然后RecycleViewitem才会开始滚动,向下滑动时当RecycleViewitem到达顶部时AppBarLayout中的Toolbar才会开始展示。注意两点:第一点,如果使用了其他值,必定要使用这个值才能起作用;第二点:如果在这个Toolbar前面的任何其他child View没有设置这个值,那么这个Toolbar设置的任何属性都将会失去作用

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible"
            app:layout_scrollFlags="scroll"
            app:title="scroll"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvToDoList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

如图:


1578541857903.gif

注意上文中的第二点:如果在这个Toolbar前面的任何其他child View没有设置这个值,那么这个Toolbar设置的任何属性都将会失去作用

比如下面的代码TextViewToolbar的前一个child view,并且TextView中并没有设置app:layout_scrollFlags="scroll",这就导致Toolbar中设置的scroll是无效的

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="15dp"
            android:gravity="center_vertical"
            android:text="child view 2"
            android:textColor="#000000"
            android:textSize="20sp"
            android:textStyle="bold"/>

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:visibility="visible"
            app:layout_scrollFlags="scroll"
            app:title="scroll"/>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvToDoList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

如图:


1578542300006.gif

enterAlways

屏幕向上滑动时AppBarLayout中的Toolbar先被隐藏然后RecycleViewitem才会开始滚动,向下滑动时先将Toolbar展示,然后RecycleViewitem才会开始滚动。这里要注意的是一定要设置scroll这样才能让enterAlways产生效果

app:layout_scrollFlags="scroll|enterAlways"

如图:


1578542503353.gif

enterAlwaysCollapsed

enterAlwaysCollapsedenterAlways的附加值,因此要同时设置enterAlwaysenterAlwaysCollapsed,并且要设置一个新的参数android:minHeight="",屏幕向上滑动时AppBarLayout中的Toolbar
先被隐藏然后RecycleViewitem才会开始滚动,向下滑动时先展示android:minHeight=""中设置的高度然后将RecycleViewitem滚动至顶部,最后将Toolbar剩余高度展示。

 app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"

如图:


1578542359254.gif

exitUntilCollapsed

exitUntilCollapsed类似enterAlwaysCollapsed但是效果正好相反,滑动时始终保留android:minHeight=""设置的最小高度,向下滑动时到达RecycleViewitem顶部时才会将Toolbar剩余部分展示出来

app:layout_scrollFlags="scroll|exitUntilCollapsed"

如图:


1578542325287.gif

snap

snap是一个吸附效果类似于 ViewPager

app:layout_scrollFlags="scroll|snap"

如图:


1578542382670.gif

相关文章

网友评论

      本文标题:AppBarLayout的五种ScrollFlags

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