An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# ViewBinding Sample

![Android](https://img.shields.io/badge/Android-3DDC84?style=for-the-badge&logo=android&logoColor=white)
![Kotlin](https://img.shields.io/badge/kotlin-%237F52FF.svg?style=for-the-badge&logo=kotlin&logoColor=white)

## 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) }
}

```