我发现我们可以使用很酷的标志,通过使用layout_scrollFlags滚动工具栏甚至内容。就我而言,我的布局是这样的:

<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">

        <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="snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

我的一个标签是fragment,其布局中有一个Recycle View,在RecycleView下面带有edittext。首先我想知道这个标志是什么意思

谷歌说:
  • SCROLL_FLAG_ENTER_ALWAYS
    进入(在屏幕上滚动)时,无论滚动 View 是否也在滚动,该 View 都会在任何向下滚动事件上滚动。
  • SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED
    “enterAlways”的附加标志将返回的 View 修改为仅最初滚动回到其折叠高度。
  • SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
    退出时(滚动到屏幕之外), View 将一直滚动,直到被“折叠”为止。
  • SCROLL_FLAG_SCROLL
    该 View 将与滚动事件直接相关地滚动。
  • SCROLL_FLAG_SNAP
    滚动结束后,如果 View 仅部分可见,则它将被捕捉并滚动到最接近的边缘。

  • 我随机更改了此标志,在某些情况下,我的编辑文本消失了,直到我向上滚动工具栏,然后出现编辑。我阅读了Google文档
    但我做得不好。我想简单地理解它。

    最佳答案

    我不知道我的答案是否仍然有用,但是。实际上,文档足以理解周围发生的事情,您只需要进行一些操作即可。
    必须启用在属性scroll中使用的app:layout_scrollFlags标志,滚动效果才能生效。此标志必须与enterAlwaysenterAlwaysCollapsedexitUntilCollapsedsnap一起启用:

  • enterAlways:向上滚动时该 View 将变为可见。在从列表底部滚动并希望在向上滚动时立即显示工具栏的情况下,此标志很有用。
  • enterAlwaysCollapsed:通常,仅使用enterAlways时,工具栏会随着向下滚动而继续扩展。假设声明了enterAlways并指定了minHeight,则还可以指定enterAlwaysCollapsed。使用此设置时,您的 View 将仅以该最小高度显示。仅当滚动到达顶部时, View 才会扩展到其完整高度
  • exitUntilCollapsed:设置了滚动标记后,向下滚动通常会导致整个内容移动。通过指定minHeight和exitUntilCollapsed,将达到工具栏的最小高度,然后其余内容开始滚动并退出屏幕
  • snap:使用此选项将确定仅部分缩小 View 时的处理方式。如果滚动结束并且 View 尺寸已减小到其原始尺寸的50%以下,则此 View 将返回其原始尺寸。如果尺寸大于其尺寸的50%,它将完全消失。

  • 请看看这个blog,它应该真的很有帮助。
    更新:另外还有关于滚动标志的article (edit: now necessary to sign in to Medium account)。非常感谢Martin Ombura Jr!

    10-07 19:17
    查看更多