Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store and restore the scroll position of collapsing toolbar during fragment changes?

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>
like image 382
Ansh Avatar asked Oct 17 '18 19:10

Ansh


People also ask

How do I fix scroll position on top of layout?

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.

How do I collapse collapsing Toolbar programmatically?

Android: Programmatically Collapse and expand the CollapsingToolbarLayout. mAppBarLayout. setExpanded(true) to expand Toolbar and use mAppBarLayout. setExpanded(false) to collapse Toolbar.

What is collapsing Toolbar layout?

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 .


1 Answers

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.

like image 143
Ansh Avatar answered Sep 26 '22 23:09

Ansh