Last active
March 30, 2021 16:11
-
-
Save johnkil/9c895c636acf8a1ae032194db45fb6fe to your computer and use it in GitHub Desktop.
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 CompositeOnFocusChangeListener constructor( | |
vararg listeners: OnFocusChangeListener | |
) : OnFocusChangeListener, MutableSet<OnFocusChangeListener> by listeners.toMutableSet() { | |
override fun onFocusChange(view: View, hasFocus: Boolean) { | |
forEach { it.onFocusChange(view, hasFocus) } | |
} | |
} | |
fun View.addOnFocusChangeListener(listener: OnFocusChangeListener) { | |
val compositeFocusChangeListener = | |
(onFocusChangeListener as? CompositeOnFocusChangeListener) ?: CompositeOnFocusChangeListener().apply { | |
onFocusChangeListener?.let(::add) | |
onFocusChangeListener = this | |
} | |
compositeFocusChangeListener.add(listener) | |
} | |
fun View.removeOnFocusChangeListener(listener: OnFocusChangeListener) { | |
(onFocusChangeListener as? CompositeOnFocusChangeListener)?.remove(listener) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment