Last active
August 22, 2024 06:23
-
-
Save paulocaldeira17/1fa85d40765245ab26552911acd385ed to your computer and use it in GitHub Desktop.
Android AppBarLayout collapsed/expanded state listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.design.widget.AppBarLayout; | |
/** | |
* App bar collapsing state | |
* @author Paulo Caldeira <[email protected]>. | |
*/ | |
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener { | |
// State | |
public enum State { | |
EXPANDED, | |
COLLAPSED, | |
IDLE | |
} | |
private State mCurrentState = State.IDLE; | |
@Override | |
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) { | |
if (i == 0) { | |
if (mCurrentState != State.EXPANDED) { | |
onStateChanged(appBarLayout, State.EXPANDED); | |
} | |
mCurrentState = State.EXPANDED; | |
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) { | |
if (mCurrentState != State.COLLAPSED) { | |
onStateChanged(appBarLayout, State.COLLAPSED); | |
} | |
mCurrentState = State.COLLAPSED; | |
} else { | |
if (mCurrentState != State.IDLE) { | |
onStateChanged(appBarLayout, State.IDLE); | |
} | |
mCurrentState = State.IDLE; | |
} | |
} | |
/** | |
* Notifies on state change | |
* @param appBarLayout Layout | |
* @param state Collapse state | |
*/ | |
public abstract void onStateChanged(AppBarLayout appBarLayout, State state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment