9

Currently, I have the following page with ViewPager

enter image description here

When the page in INFO tab is scrolled, toolbar will be hidden. This behavior is implemented via CoordinatorLayout, AppBarLayout and app:layout_scrollFlags

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:elevation="0dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

        <android.support.v7.widget.Toolbar
            app:layout_scrollFlags="scroll|enterAlways|snap"

            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:elevation="0dp"
            android:elevation="0dp"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="?attr/detailedStockTabIndicatorColor" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

So, this is how it looks like after scrolling.

After Scroll, Toolbar is hidden

enter image description here

Since this is a ViewPager, if I swipe to FINANCIAL tab, it will look like the following.

Followed by Swipe

enter image description here

Since, the page in FINANCIAL tab is not scroll-able, we hope not to hide the Toolbar.

I was wondering, how to make toolbar visible again which is previous hidden using layout_scrollFlags, when swipe to different ViewPager page?

Marzi Heidari
  • 2,660
  • 4
  • 25
  • 57
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

5 Answers5

5

you can do it like this :

final AppBarLayout appBarLayout = view.findViewById(R.id.app_bar_layout);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 1)
                appBarLayout.setExpanded(true, true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

so when viewPager is in unscrollable page it expands and toolbar appears

Minato
  • 4,383
  • 1
  • 22
  • 28
Marzi Heidari
  • 2,660
  • 4
  • 25
  • 57
2

@Marzieh Heidari answer is the correct answer for this question, but I share my different approach that I use in my project to solve this problem.

In the fragment which have short content, I still keep NestedScrollView at root. Then I still able to scroll up/down to collapse and expand toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:isScrollContainer="false"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This fragment have short content"
            android:textSize="100sp"
            />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

Hope it help

Linh
  • 57,942
  • 23
  • 262
  • 279
0

Maybe the application life cycle events can help you here, by catching the load of the new screen and set the toolbar at that point programmaticly to show?

Leo Muller
  • 1,421
  • 1
  • 12
  • 20
0

You need to expand toolbar in when user swipe to second tab and make toolbar unscrollable

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position == 1) {
            appbar.setExpanded(true, true);

            AppBarLayout.LayoutParams params =
                    (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
            params.setScrollFlags(0);   // clear flags
            toolbar.setLayoutParams(params);
        } else {
            AppBarLayout.LayoutParams params =
                    (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
            params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
                    | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
            toolbar.setLayoutParams(params);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});
SiSa
  • 2,594
  • 1
  • 15
  • 33
0

You asked duplicate question. your question's answer available here

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            expandToolbar();
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

public void expandToolbar(){
    AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.app_bar_layout);
    appBarLayout.setExpanded(true, true);
}
SANAT
  • 8,489
  • 55
  • 66