Created
August 30, 2019 09:12
-
-
Save twiceyuan/7a2248b122f8d157852e68b130534c49 to your computer and use it in GitHub Desktop.
[键盘显示隐藏监听] #Android
This file contains hidden or 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
class KeyboardVisibilityListener( | |
private val activity: Activity, | |
private val rootLayout: ViewGroup, | |
private val listener: (isShow: Boolean) -> Unit | |
) { | |
private val keyboardLayoutListener = ViewTreeObserver.OnGlobalLayoutListener { | |
val heightDiff = rootLayout.rootView.height - rootLayout.height | |
val contentViewTop = activity.window.findViewById<View>(ID_ANDROID_CONTENT).top | |
listener(heightDiff > contentViewTop) | |
} | |
init { | |
rootLayout.viewTreeObserver.addOnGlobalLayoutListener(keyboardLayoutListener) | |
} | |
fun release() { | |
rootLayout.viewTreeObserver.removeOnGlobalLayoutListener(keyboardLayoutListener) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment