2

I have search the Stack Overflow beforewise, but haven't found an appropriate answer to my problem.

I have an Android application with the coordinator layout that has a nested ViewPager inside. If I scroll a RecyclerView that is inside of the first fragment in a View pager, the Toolbar is hidden and shown as intended. However, my other fragments in the ViewPager do not have nested scroll, so I would like to show Toolbar if it is hidden on ViewPager page change. I wonder if I can extend the CoordinatorLayout behavior to have it nicely done.

Thanks in advance! I will be happy to provide more details if needed.

The approximate code is (tried to strip all the unnecessary stuff): 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>

and a scrolling fragment: 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"/>

I have found a couple of related questions like: this or this. But they focus mostly on layout problems, while I want to understand if it is real to have a nice solution to triggering Toolbar movement on demand.

Community
  • 1
  • 1
niosus
  • 738
  • 8
  • 22

1 Answers1

6

So it seems that the answer is the following: wrap the Toolbar in AppBarLayout and in the code use something like: appBarLayout.setExpanded(false, true);

This does the trick for me.

niosus
  • 738
  • 8
  • 22