BindView - VerstSiu/kotlin_extension GitHub Wiki
package com.ijoic.ktx.widget.bind
/*
* Bind once
*/
fun<T: View> Fragment.bindOnce(@IdRes id: Int): ReadOnlyProperty<Fragment, T>
fun<T: View> Fragment.bindOnceOptional(@IdRes id: Int): ReadOnlyProperty<Fragment, T?>
fun<T: View> Activity.bindOnce(@IdRes id: Int): ReadOnlyProperty<Activity, T>
fun<T: View> Activity.bindOnceOptional(@IdRes id: Int): ReadOnlyProperty<Activity, T?>
fun<T: View> ViewHolder.bindOnce(@IdRes id: Int): ReadOnlyProperty<ViewHolder, T>
fun<T: View> ViewHolder.bindOnceOptional(@IdRes id: Int): ReadOnlyProperty<ViewHolder, T?>
/*
* Bind view
*/
interface ViewSource {
fun<T: View> findViewById(@IdRes id: Int): T?
}
interface FragmentViewSource: ViewSource {
val view: View?
}
fun<T: View> ViewSource.bindView(@IdRes id: Int): ReadOnlyProperty<ViewSource, T>
fun<T: View> ViewSource.bindViewOptional(@IdRes id: Int): ReadOnlyProperty<ViewSource, T?>
fun ViewSource.releaseBindViews()
bind once:
class MyFragment: Fragment() {
private val messageView: TextView by bindOnce(R.id.message_text)
override fun onActivityCreated() {
super.onActivityCreated()
messageView.text = "Hello world!"
}
}
bind view:
class MyFragment: Fragment(), FragmentViewSource {
private val messageView: TextView by bindView(R.id.message_text)
override fun onActivityCreated() {
super.onActivityCreated()
// reset view binding when root view is destroyed/recreated
releaseBindViews()
messageView.text = "Hello world!"
}
}