Created
April 22, 2022 07:40
-
-
Save hiking93/72f415600ff3c6f459d6dc8fcd99f541 to your computer and use it in GitHub Desktop.
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
fun View.doOnWindowInsetsChanged( | |
listenToAnimation: Boolean = false, | |
@DispatchMode dispatchMode: Int = DISPATCH_MODE_STOP, | |
callback: (v: View, insets: WindowInsetsCompat) -> WindowInsetsCompat, | |
) { | |
var isAnimationRunning = false | |
ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets -> | |
if (isAnimationRunning) insets else callback(v, insets) | |
} | |
if (listenToAnimation) { | |
ViewCompat.setWindowInsetsAnimationCallback( | |
this, | |
object : WindowInsetsAnimationCompat.Callback(dispatchMode) { | |
override fun onPrepare(animation: WindowInsetsAnimationCompat) { | |
super.onPrepare(animation) | |
isAnimationRunning = true | |
} | |
override fun onProgress( | |
insets: WindowInsetsCompat, | |
runningAnimations: MutableList<WindowInsetsAnimationCompat> | |
) = callback(this@doOnWindowInsetsChanged, insets) | |
override fun onEnd(animation: WindowInsetsAnimationCompat) { | |
super.onEnd(animation) | |
isAnimationRunning = false | |
} | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment