How to make toolbar visible again which is previous hidden using layout_scrollFlags, when swipe to different viewpager page [duplicate]
Asked Answered
C

5

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?

Chetnik answered 26/9, 2017 at 20:43 Comment(0)
X
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

Xylophone answered 28/9, 2017 at 22:24 Comment(2)
To remind, the position no. should be 1 because the requesting expand toolbar is the "FINANCIAL" tabHusband
@LongRanger editted. thanks :)Xylophone
E
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

Exo answered 29/9, 2017 at 8:1 Comment(0)
T
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?

Tadzhik answered 3/10, 2017 at 12:17 Comment(0)
L
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) {

    }
});
Larch answered 3/10, 2017 at 15:23 Comment(1)
This is the exact same I answered... yesterday..Unhitch
O
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);
}
Osterman answered 5/10, 2017 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.