在ViewPager中切换页面时显示工具栏

nio*_*sus 2 android android-layout android-fragments android-recyclerview android-coordinatorlayout

我之前搜索过堆栈溢出,但没有找到适合我的问题的答案.

我有一个带有协调器布局的Android应用程序,里面有一个嵌套的ViewPager.如果我在View分页器中滚动第一个片段内的RecyclerView,则隐藏工具栏并按预期显示.但是,我在ViewPager中的其他片段没有嵌套滚动,所以我想显示工具栏是否隐藏在ViewPager页面更改上.我想知道我是否可以扩展CoordinatorLayout行为以完成它.

提前致谢!如果需要,我很乐意提供更多细节.

大概的代码是(试图剥离所有不必要的东西): main_activity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/toolbar" />
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tab_layout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

和滚动片段: fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.v7.widget.CardView
    android:id="@+id/add_word_card"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <RelativeLayout
        android:id="@+id/add_word_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/highlight">
        <!-- some unrelated stuff -->
    </RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_below="@id/add_word_card"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:context=".MainActivity"/>
Run Code Online (Sandbox Code Playgroud)

我发现了一些相关的问题:这个或者这个.但是他们主要关注布局问题,而我想了解是否真的有一个很好的解决方案来按需触发工具栏移动.

nio*_*sus 6

所以似乎答案如下:在Toolbarin AppBarLayout和代码中使用类似的东西:appBarLayout.setExpanded(false, true);

这对我来说很有用.