我正在开发的应用程序包括一个导航抽屉,该抽屉已在 Activity 中实现。该 Activity 的布局如下:
<FrameLayout 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.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include
android:id="@+id/appbar"
layout="@layout/appbar" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header_drawer"
app:menu="@menu/menu_nav">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
这是一个非常常见的模式,唯一经常更改的是容器布局内的Fragment。如果任何的Fragment具有滚动元素,则在滚动时,CoordinatorLayout将愉快地进行位置平移,包括Toolbar / AppBarLayout。
真正的问题在于,当Fragment被替换时,Toolbar的位置保持不变,即使Toolbar被隐藏,它也会保持不变,这并不是预期的结果。
结果如下:
这个:
得到卡住了:
如何为这种情况重置Toolbar的位置?
编辑: 可能存在一个错误,AppBarLayout偏移更改侦听器仅在重新启动应用程序(按返回按钮并打开应用程序)时调用,并在强烈的挥舞后停止再次调用。
app:layout_scrollFlags="scroll"
。 - FroyomAppBar.addOnOffsetChangedListener(this);
和@Override public void onOffsetChanged(AppBarLayout appBarLayout, int offset) { if(recycler.canScrollVertically(1)) { appBarLayout.setTranslationY(0); } }
代码块。基本上,检查你的滚动视图是否可以滚动并锁定 AppBarLayout。 - razzledazzle