我希望工具栏在我向上滚动回收器视图时完全隐藏,并在向下滚动时可见。但是当我滚动回收器视图时工具栏不会隐藏,工具栏只是保持固定。
这是我的代码:
main_actionbar.xml 文件包含工具栏,我在 activity_main.xml 中使用了它。 activity_main.xml 布局 main_actionbar.xml 布局<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/veryLightGrey"
tools:context=".activities.MainActivity">
<include
android:id="@+id/include"
layout="@layout/main_actionbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notes_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:background="@color/veryLightGrey"
android:paddingStart="5dp"
android:paddingLeft="5dp"
android:scrollbars="vertical"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="100dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/dusty_grey">
<androidx.appcompat.widget.Toolbar
android:id="@+id/mainTobar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
android:id="@+id/action_barName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="note"
/>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>