Collapsing Toolbar Layout always expanded when returned from fragment
T

5

10

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?

Typhoon answered 14/12, 2016 at 13:7 Comment(0)
F
5

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);
Fattish answered 21/10, 2018 at 16:23 Comment(1)
Explain what you did?Fattish
G
3

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.

Gerrilee answered 17/4, 2020 at 15:42 Comment(0)
S
2

Today, to fix this issue you only have to set an id to your coordinator layout to fix this.

Sheryllshetland answered 29/9, 2021 at 19:47 Comment(0)
G
1

I had face same problem so i write below code:-

private boolean isExpand = true;
private void setTitleNotExpand(boolean isExpand) {
    if(getFragmentViewHolder() != null) {
        this.isExpand = isExpand;
        // AppBarLayout variable
        getFragmentViewHolder().appbar.setExpanded(isExpand);
    }
}

when you do add back stack then write below code :-

// write below code where you want to stick your toolbar
setTitleNotExpand(false);


// write below code where you want not to stick your toolbar
setTitleNotExpand(true);

on your onFragmentViewHolderCreated write below code :-

getFragmentViewHolder().appbar.setExpanded(isExpand);
Georgettageorgette answered 12/1, 2017 at 7:52 Comment(0)
L
1

another thing to consider is that views cannot restore their state if they don't have an id

Lollop answered 14/5, 2021 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.