如何重置由CoordinatorLayout控制工具栏位置? [英] How to reset the Toolbar position controlled by the CoordinatorLayout?

查看:1509
本文介绍了如何重置由CoordinatorLayout控制工具栏位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序我工作的一个组成抽屉式导航是一个活动来实现的。活动安排如下:

The app I'm working on consists of a Navigation Drawer which is implemented in an Activity. The activity layout is as follows:

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

这是一个很常见的模式,改变经常是片断的容器布局中唯一的事情。

This is a very common pattern, only thing that changes frequently is the Fragment inside the container layout.

如果任何片段都具有滚动元件,在滚动时,CoordinatorLayout会很乐意进行位置转换,包括工具栏/ AppBarLayout。

If any of the Fragment has a scrolling element, upon scrolling, the CoordinatorLayout will happily make position translations, including the Toolbar/AppBarLayout.

真正的问题就在这里,当片段被替换,对于工具栏的位置保持不变,也就是说,如果工具栏被隐藏,它会保持如此 这并不意。

The real problem here is, when the Fragment gets replaced, the position for the Toolbar remains the same, i.e., if the Toolbar is hidden, it will stay so which isn't intended.

结果是这样的:

本:

卡:

一个人怎么可以重置工具栏的位置,对于这种情况?

How can one reset the Toolbar position for this case?

编辑: 一个错误是可能的,在AppBarLayout偏移重新推出时,应用程序(preSS后退按钮,打开应用程序),并停止越来越激烈一扔后再次调用更改侦听器仅获取被调用。

A bug is probable, the AppBarLayout offset change listener gets called only when relaunching the app (press back button and open the app), and stops getting called again after an intense fling.

推荐答案

要重置滚动状态,只得到了 AppBarLayout.Behavior 对象

To reset the scroll state, just get the AppBarLayout.Behavior object

CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator);
AppBarLayout appbar = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();       

和调用 onNested preScroll 手动方式:

int[] consumed = new int[2];
behavior.onNestedPreScroll(coordinator, appbar, null, 0, -1000, consumed);

如果您想与动画顺利复位,你可以尝试调用 onNestedFling 来代替:

If you would like to reset smoothly with an animation, you can try calling onNestedFling instead:

behavior.onNestedFling(coordinator, appbar, null, 0, -1000, true);

这篇关于如何重置由CoordinatorLayout控制工具栏位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆