Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsing Toolbar Layout always expanded when returned from fragment

I have 2 fragments (Fragment A and Fragment B) both with collapsing toolbar layouts and corresponding recyclerviews within a coordinatorlayout.

If I scroll up in my recyclerview (so the CollapsingToolbarLayout has collapsed) and then open Fragment B from fragment A (Pushing A onto the backstack).

When I return to fragment A by hitting back. The CollapsingToolbarLayout/AppBarLayout is always expanded, even though the recycler view is in the same position.

Anyone experience this?

like image 896
FlashAsh80 Avatar asked Dec 14 '16 13:12

FlashAsh80


People also ask

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 .

How do I collapse collapsing Toolbar programmatically?

How do I collapse collapsing Toolbar programmatically? Use mAppBarLayout. setExpanded(true) to expand Toolbar and use mAppBarLayout. setExpanded(false) to collapse Toolbar.


2 Answers

There's a issue related to this. According to Chris Banes.

Add these lines inside onViewCreated() of your fragment to solve the issue.

ViewCompat.requestApplyInsets(mCoordinatorLayout);
like image 105
Ansh Avatar answered Nov 05 '22 16:11

Ansh


It's an old question but none of the answers it's correct. I found this other question where they fixed the problem:

How to restore Collapsing Toolbar Layout State after screen orientation change

You have to set an id to your views in order to restore their state automatically.

like image 36
Gonzalo Avatar answered Nov 05 '22 16:11

Gonzalo