Android 详细分析AppBarLayout的五种ScrollFlags

AidaMattiso 8年前
   <p>在前面两篇MD系列的文章中,通过两个案例基本上能够掌握了CoordinatorLayout与AppBarLayout的配合使用。本文我们回过头来详细聊聊AppBarLayout的ScrollFlags属性,了解一下不同值之间的区别。至此,Android Material Design系列的学习已进行到第七篇,大家可以点击以下链接查看之前的文章:</p>    <ul>     <li><a href="/misc/goto?guid=4959713508147155686" rel="nofollow,noindex">Android TabLayout 分分钟打造一个滑动标签页</a></li>     <li><a href="http://www.open-open.com/lib/view/open1470728486808.html">Android 一文告诉你到底是用Dialog,Snackbar,还是Toast</a></li>     <li><a href="/misc/goto?guid=4959713508247750167" rel="nofollow,noindex">Android FloatingActionButton 重要的操作不要太多,一个就好</a></li>     <li><a href="/misc/goto?guid=4959713508327781996" rel="nofollow,noindex">Android 初识AppBarLayout 和 CoordinatorLayout</a></li>     <li><a href="/misc/goto?guid=4959713508414759927" rel="nofollow,noindex">Android CoordinatorLayout实战案例学习《一》</a></li>     <li><a href="/misc/goto?guid=4959713508494498386" rel="nofollow,noindex">Android CoordinatorLayout 实战案例学习《二》</a></li>    </ul>    <p>ScrollFlags共有五种常量值供AppBarLayout的Children View使用,在xml布局文件中通过 app:layout_scrollFlags 设置,对应的值为:scroll,enterAlways,enterAlwaysCollapsed,snap,exitUntilCollapsed;也可以在代码中通过 setScrollFlags(int) 方法使用,比如:</p>    <pre>  <code class="language-java">Toolbar toolbar = ... // your toolbar within an AppBarLayout  AppBarLayout.LayoutParams params =       (AppBarLayout.LayoutParams) toolbar.getLayoutParams();  params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL      | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);</code></pre>    <p>下面我们通过官网介绍、XML代码和对应的效果图分别分析这五种值的使用(备注:代码中设置也一样,不再赘述):</p>    <h2>scroll</h2>    <p>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.</p>    <p>Child View 伴随着滚动事件而滚出或滚进屏幕。注意两点:第一点,如果使用了其他值,必定要使用这个值才能起作用;第二点:如果在这个child View前面的任何其他Child View没有设置这个值,那么这个Child View的设置将失去作用。</p>    <p>示例XML代码:</p>    <pre>  <code class="language-java"><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              xmlns:app="http://schemas.android.com/apk/res-auto"              android:id="@+id/tb_toolbar"              android:layout_width="match_parent"              android:layout_height="@dimen/dp_56"              app:titleTextColor="@color/white"              app:title="@string/app_name"              app:theme="@style/OverFlowMenuTheme"              app:popupTheme="@style/AppTheme"              android:background="@color/blue"              app:layout_scrollFlags="scroll|exitUntilCollapsed"/>        </android.support.design.widget.AppBarLayout></code></pre>    <p>对应效果图:</p>    <p><img src="https://simg.open-open.com/show/75d30ba96430288b1fbf7ffb1a222d4e.gif"></p>    <p>Samples01.gif</p>    <h2>enterAlways</h2>    <p>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.</p>    <p>快速返回模式。其实就是向下滚动时Scrolling View和Child View之间的滚动优先级问题。对比 scroll 和 scroll | enterAlways 设置,发生向下滚动事件时,前者优先滚动Scrolling View,后者优先滚动Child View,当优先滚动的一方已经全部滚进屏幕之后,另一方才开始滚动。</p>    <p>示例XML代码:</p>    <pre>  <code class="language-java">...  app:layout_scrollFlags="scroll|enterAlways"  ...</code></pre>    <p>对应效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/a43756e8fbc35a88d8d72488e44e7df8.gif"></p>    <p style="text-align:center">Samples02.gif</p>    <h2>enterAlwaysCollapsed</h2>    <p>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.</p>    <p>enterAlways的附加值。这里涉及到Child View的高度和最小高度,向下滚动时,Child View先向下滚动最小高度值,然后Scrolling View开始滚动,到达边界时,Child View再向下滚动,直至显示完全。</p>    <p>示例XML代码:</p>    <pre>  <code class="language-java">...  android:layout_height="@dimen/dp_200"  android:minHeight="@dimen/dp_56"  ...  app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"  ...</code></pre>    <p>对应效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/ed1628f3b3f38873e6cd77c41a0fcbb7.gif"></p>    <p style="text-align:center">Samples03.gif</p>    <h2>exitUntilCollapsed</h2>    <p>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.</p>    <p>这里也涉及到最小高度。发生向上滚动事件时,Child View向上滚动退出直至最小高度,然后Scrolling View开始滚动。也就是,Child View不会完全退出屏幕。</p>    <p>示例SML代码:</p>    <pre>  <code class="language-java">...  android:layout_height="@dimen/dp_200"  android:minHeight="@dimen/dp_56"  ...  app:layout_scrollFlags="scroll|exitUntilCollapsed"  ...</code></pre>    <p>对应效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/05bcabd797eec53f5e904dab1dc5fc32.gif"></p>    <p style="text-align:center">Samples04.gif</p>    <h2>snap</h2>    <p>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.</p>    <p>简单理解,就是Child View滚动比例的一个吸附效果。也就是说,Child View不会存在局部显示的情况,滚动Child View的部分高度,当我们松开手指时,Child View要么向上全部滚出屏幕,要么向下全部滚进屏幕,有点类似ViewPager的左右滑动。</p>    <p>示例XML代码:</p>    <pre>  <code class="language-java">...  android:layout_height="@dimen/dp_200"  ...  app:layout_scrollFlags="scroll|snap"  ...</code></pre>    <p>对应效果图:</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/59f4249db7e90f8fa129669f91fa4038.gif"></p>    <p style="text-align:center">Samples05.gif</p>    <h2> </h2>    <p> </p>    <p>来自:http://www.jianshu.com/p/7caa5f4f49bd</p>    <p> </p>