40 lines
1.1 KiB
Kotlin
40 lines
1.1 KiB
Kotlin
|
package code.name.monkey.retromusic.misc
|
||
|
|
||
|
import com.google.android.material.appbar.AppBarLayout
|
||
|
|
||
|
/**
|
||
|
* @author Hemanth S (h4h13).
|
||
|
* https://stackoverflow.com/a/33891727
|
||
|
*/
|
||
|
|
||
|
abstract class AppBarStateChangeListener : AppBarLayout.OnOffsetChangedListener {
|
||
|
|
||
|
private var mCurrentState = State.IDLE
|
||
|
|
||
|
override fun onOffsetChanged(appBarLayout: AppBarLayout, i: Int) {
|
||
|
if (i == 0) {
|
||
|
if (mCurrentState != State.EXPANDED) {
|
||
|
onStateChanged(appBarLayout, State.EXPANDED)
|
||
|
}
|
||
|
mCurrentState = State.EXPANDED
|
||
|
} else if (Math.abs(i) >= appBarLayout.totalScrollRange) {
|
||
|
if (mCurrentState != State.COLLAPSED) {
|
||
|
onStateChanged(appBarLayout, State.COLLAPSED)
|
||
|
}
|
||
|
mCurrentState = State.COLLAPSED
|
||
|
} else {
|
||
|
if (mCurrentState != State.IDLE) {
|
||
|
onStateChanged(appBarLayout, State.IDLE)
|
||
|
}
|
||
|
mCurrentState = State.IDLE
|
||
|
}
|
||
|
}
|
||
|
|
||
|
abstract fun onStateChanged(appBarLayout: AppBarLayout, state: State)
|
||
|
|
||
|
enum class State {
|
||
|
EXPANDED,
|
||
|
COLLAPSED,
|
||
|
IDLE
|
||
|
}
|
||
|
}
|