Currently, I have the following page with ViewPager
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.
Since this is a ViewPager
, if I swipe to FINANCIAL tab, it will look like the following.
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?
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
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With