Skip to content

Instantly share code, notes, and snippets.

@twiceyuan
Created August 30, 2019 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twiceyuan/7a2248b122f8d157852e68b130534c49 to your computer and use it in GitHub Desktop.
Save twiceyuan/7a2248b122f8d157852e68b130534c49 to your computer and use it in GitHub Desktop.
[键盘显示隐藏监听] #Android
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