https://github.com/ashwagandha-coder/viewbinding
demo init
https://github.com/ashwagandha-coder/viewbinding
android kotlin viewbinding
Last synced: 23 days ago
JSON representation
demo init
- Host: GitHub
- URL: https://github.com/ashwagandha-coder/viewbinding
- Owner: Ashwagandha-coder
- License: mit
- Created: 2022-12-21T09:13:11.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-21T13:15:41.000Z (over 3 years ago)
- Last Synced: 2025-06-01T10:42:50.195Z (12 months ago)
- Topics: android, kotlin, viewbinding
- Language: Kotlin
- Homepage:
- Size: 118 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ViewBinding Sample


## Intro
- ViewBinding is library which generation Java code for easy using View elements
``` Kotlin
private fun bind() {
binding.tvMain.text = getString(R.string.hello_second_fragment)
}
```
## Initialization via Delegates
``` Kotlin
private class FragmentViewBindingProperty(
viewBinder: (F) -> T
) : ViewBindingProperty(viewBinder) {
override fun getLifecycleOwner(thisRef: F) = thisRef.viewLifecycleOwner
}
/**
* Create new [ViewBinding] associated with the [Fragment]
*/
@JvmName("viewBindingFragment")
public fun Fragment.viewBinding(viewBinder: (F) -> T): ViewBindingProperty {
return FragmentViewBindingProperty(viewBinder)
}
/**
* Create new [ViewBinding] associated with the [Fragment]
*
* @param vbFactory Function that create new instance of [ViewBinding]. `MyViewBinding::bind` can be used
* @param viewProvider Provide a [View] from the Fragment. By default call [Fragment.requireView]
*/
@JvmName("viewBindingFragment")
public inline fun Fragment.viewBinding(
crossinline vbFactory: (View) -> T,
crossinline viewProvider: (F) -> View = Fragment::requireView
): ViewBindingProperty {
return viewBinding { fragment: F -> vbFactory(viewProvider(fragment)) }
}
/**
* Create new [ViewBinding] associated with the [Fragment]
*
* @param vbFactory Function that create new instance of [ViewBinding]. `MyViewBinding::bind` can be used
* @param viewBindingRootId Root view's id that will be used as root for the view binding
*/
@JvmName("viewBindingFragment")
public inline fun Fragment.viewBinding(
crossinline vbFactory: (View) -> T,
@IdRes viewBindingRootId: Int
): ViewBindingProperty {
return viewBinding(vbFactory) { fragment: Fragment -> fragment.requireView().findViewById(viewBindingRootId) }
}
```