25

I have a fragment that uses the new CoordinatorLayout/AppBarLayout/CollapsingToolbarLayout paradigm, and I'd like to be able to detect when the collapsing toolbar is fully expanded so that I can perform an operation on the entire fragment it's in, e.g. popping the fragment off the stack and going to a new one, dismissing the fragment. I have the dismissing code working, I just need to know when and when not to use it.

I've experimented a bit with AppBarLayout.OnOffsetChangedListener, but didn't have much luck. Is there a way to use it to determine when things are completely expanded, or is there a more preferred method someone knows about?

Thanks in advance!

EDIT: I also see there are a couple implementations for AppBarLayout.setExpanded(...), however not AppBarLayout.getExpanded() or something similar, so I'm stumped there too.

James Pizzurro
  • 365
  • 1
  • 3
  • 10
  • maybe appBarLayout.addOnOffsetChangedListener could help but i found it buggy propably i didn't implement it correctly. – mstrengis Aug 25 '15 at 20:57
  • Did you find a solution for this? I've run into the same problem trying to prevent a ScrollRefreshLayout from refreshing when the toolbar is partly collapsed. – Jimeux Sep 23 '15 at 12:37

3 Answers3

49

It doesn't look like there's anything in the APIs, but the following seems to be working for me. It might need testing.

boolean fullyExpanded =
    (appBarLayout.getHeight() - appBarLayout.getBottom()) == 0;

Edit: The above solution does seem to work, but since I wanted to test this condition when the appbar was scrolled, I ended up using the following solution with OnOffsetChangedListener.

class Frag extends Fragment implements AppBarLayout.OnOffsetChangedListener {

    private boolean appBarIsExpanded = true;
    private AppBarLayout appBarLayout;

    @Override 
    public void onActivityCreated(Bundle state) {
        super.onActivityCreated(state);
        appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.app_bar);
    }

    @Override
    public void onResume() {
        super.onResume();
        appBarLayout.addOnOffsetChangedListener(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        appBarLayout.removeOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        appBarIsExpanded = (verticalOffset == 0);
    } 

}
Jimeux
  • 2,956
  • 1
  • 18
  • 14
  • I ended up abandoning the concept behind why I wanted to do this in the first place, but I'll accept your answer since you say it's working for you. Thanks! – James Pizzurro Sep 23 '15 at 17:22
  • this aint working for me either. added the implementation of the interface but it doesnt seem to get called – filthy_wizard Jan 19 '16 at 16:39
  • 1
    @user1232726 You need to attach the listener to the AppBarLayout. I updated the code with a way to do this. If you don't care about potential leaks, just using `appBarLayout.setOnScrollChangeListener(this)` will do the job. – Jimeux Jan 20 '16 at 01:13
  • 2
    you just saved my day. Thank you. – v4_adi Aug 02 '17 at 17:00
10

My solution is based on creating a custom view. First create a class extending the native AppBarLayout:

public class CustomAppBar extends AppBarLayout { ....

Then inside the class set an addOnOffsetChangedListener like this:

this.addOnOffsetChangedListener...

You can do the above by setting in the constructor or maybe by calling a method inside the constructor. So you need the constructor, remember to use the constructor with 2 params to be able to added to the xml:

public CustomAppBar(Context context, AttributeSet attrs) {
        super(context, attrs);
//You can set the listener here or maybe call the method that set the listener
}

Then we have to get access to the state of the view, so create a private boolean inside your custom view class, and set it to true or false if your view start expanded or collapsed, in this case my view is by default expanded:

private boolean isExpanded = true;

Now you have to update the state of that boolean:

this.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (verticalOffset == 0) {
                    isExpanded = true;
                } else {
                    isExpanded = false;
                }
            }
        });

Next step is to get the state of the boolean by using a getter inside the CustomAppBar class

public boolean isExpanded() {
        return isExpanded;
    }

The next is go to your xml, use your custom view there, then in the Acivity or Fragment get the view and use the method to know the AppBar status

cutiko
  • 9,887
  • 3
  • 45
  • 59
  • That seems like an overkill when you can just add an `OnOffsetChangedListener` directly to the `AppBarLayout` instance. – Aspiring Dev Jul 20 '16 at 16:11
  • If you are only gonna use that AppBarLayout once: yes it is. But if more than one activity need to know the AppBarLayout state, then: this is a DRY solution – cutiko Jul 20 '16 at 20:03
8

I know, that it maybe a bit late, but exploring the source code of the AppBArLayout I have found, that the AppBarLayout.OnOffsetChangedListener just translates the value of the int getTopAndBottomOffset() of the AppBar's Behaviour.

So at any time you can just use this code to define whether an AppBarLayout expanded or not:

public boolean isAppBArExpanded(AppBarLayout abl) {
    final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).getBehavior();
    return (behavior instanceof AppBarLayout.Behavior) ? (((AppBarLayout.Behavior) behavior).getTopAndBottomOffset() == 0) : false;
}