Created
September 24, 2018 07:39
-
-
Save Lamartio/1e3833740c2a5f30167a9a3995076e90 to your computer and use it in GitHub Desktop.
kotlins_simple_layout_trick_pt2_view_extensions.kt
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
| open class BindableLayout<T, V : ViewGroup>( | |
| layout: V, | |
| private val observable: Observable<T> | |
| ) : Layout<V>(layout) { | |
| init { | |
| layout.addOnAttachStateChangeListener(DisposableOnAttachStateChangeListener { | |
| observable.subscribe(observers) | |
| }) | |
| } | |
| // ... | |
| } | |
| class DisposableOnAttachStateChangeListener( | |
| private val onViewAttachedToWindow: (view: View) -> Disposable | |
| ) : View.OnAttachStateChangeListener { | |
| private var disposable: Disposable? = null | |
| override fun onViewAttachedToWindow(view: View) { | |
| disposable = onViewAttachedToWindow.invoke(view) | |
| } | |
| override fun onViewDetachedFromWindow(view: View) { | |
| disposable?.dispose() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment