Created
January 17, 2020 00:46
-
-
Save satoshun/0185c4231983016f6afa4d8f8c423cd9 to your computer and use it in GitHub Desktop.
This file contains 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 MainFragment : Fragment(R.layout.main_frag2) { | |
private val binding: MainFrag2Binding by viewBinding() | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
// ... | |
} | |
} |
This file contains 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
fun <T : ViewDataBinding> Fragment.viewBinding(): ReadOnlyProperty<Fragment, T> = | |
object : ReadOnlyProperty<Fragment, T> { | |
override fun getValue(thisRef: Fragment, property: KProperty<*>): T { | |
val view = thisRef.view!! | |
var binding = view.getTag(R.id.fragment_binding) as? T | |
if (binding == null) { | |
binding = DataBindingUtil.bind(view) | |
view.setTag(R.id.fragment_binding, binding) | |
} | |
return binding!! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment