如何在开始时隐藏搜索栏

How to hide the searchbar as it starts

我有一个用于标题和图标的顶部栏以及一个搜索栏。当我向上滚动那个隐藏的时候,它就可以了。但是我想在 activity 首先开始时隐藏搜索栏,并在首先向下滚动时显示。 (喜欢ioswhatsapp应用,默认隐藏)

<androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="56dp"
                    app:layout_scrollFlags="scroll">

                    //...

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    //...

                </RelativeLayout>

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

            <androidx.recyclerview.widget.RecyclerView/>


        </androidx.coordinatorlayout.widget.CoordinatorLayout>

是否有任何滚动标志。感谢您的帮助。

对不起我糟糕的英语。

如代码所示使用折叠工具栏

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Scrollable view here -->

      <com.google.android.material.appbar.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="@dimen/tall_toolbar_height">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="top"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

          <androidx.appcompat.widget.Toolbar
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              app:layout_collapseMode="pin"/>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
      </com.google.android.material.appbar.AppBarLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

将 app:expanded="false" 添加到应用栏布局。并且里面只用一根吧。

<RelativeLayout>
<RelativeLayout
    android:layout_height="56dp"
    android:layout_width="match_parent"
    app:layout_scrollFlags="scroll">

    //...

</RelativeLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">


    <com.google.android.material.appbar.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:expanded="false">

        <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">

            //...

        </RelativeLayout>

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

    <androidx.recyclerview.widget.RecyclerView />


</androidx.coordinatorlayout.widget.CoordinatorLayout>