2

我有搜索堆栈溢出之前,但还没有找到我的问题的适当答案。在ViewPager中切换页面时显示工具栏

我有一个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> 

以及滚动片段: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"/> 

我已经发现了几个相关问题如:thisthis。但他们主要关注布局问题,而我想了解是否真的有一个很好的解决方案来按需触发工具栏移动。

回答

5

如此看来,答案是:包裹ToolbarAppBarLayout,并在代码中使用类似:appBarLayout.setExpanded(false, true);

这是否把戏对我来说。