What I want : To save the scroll position of the collapsing toolbar when switching fragments.
Right now the toolbar recreates and doesn't save the last scrolled position but recyclerView does. How to save the collapsing toolbar scroll position?
Layout.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/f_c_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/f_c_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/f_c_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:titleEnabled="false"
app:layout_scrollFlags="scroll">
<android.support.v7.widget.Toolbar
android:id="@+id/f_c_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@android:color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
....
/>
</android.support.design.widget.CoordinatorLayout>
Inside AppBarLaout , add child CollapsingToolbarLayout and TabLayout . Keep Toolbar and ImageView into CollapsingToolbarLayout . 4. Add attribute app:layout_scrollFlags="scroll|exitUntilCollapsed" to CollapsingToolbarLayout and attribute app:layout_scrollFlags="scroll|enterAlways" to Toolbar for collapsing effect.
Android: Programmatically Collapse and expand the CollapsingToolbarLayout. mAppBarLayout. setExpanded(true) to expand Toolbar and use mAppBarLayout. setExpanded(false) to collapse Toolbar.
CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout .
After looking couple of days into StackOverflow, I actually found the solution.
I'm not sure what's causing the issue but CollapsingToolbar save it'scroll position when switching between activities. This issue occures while working with Fragments using FragmentTransaction.
To save the scrolled state of CollpasingToolbar add this line to fragment onViewCreated() method.
ViewCompat.requestApplyInsets(mCoordinatorLayout); // pass the coordinatorlayout id.
If one could explain the exact reason what's the reason behind the issue then please free to comment.
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