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

https://github.com/jrvansuita/view-binding

🖇 This is an Android library. It makes your life easier by reducing boilerplate code and avoiding typecasting and redundant lines of code when using View Binding
https://github.com/jrvansuita/view-binding

android viewbinding viewbinding-property-delegate

Last synced: 25 days ago
JSON representation

🖇 This is an Android library. It makes your life easier by reducing boilerplate code and avoiding typecasting and redundant lines of code when using View Binding

Awesome Lists containing this project

README

          

# View Binding

This is an [**Android**](https://developer.android.com) library. It makes your life easier by reducing boilerplate code and avoiding redundant lines of code when are using the [ViewBinding](https://developer.android.com/topic/libraries/view-binding) feature on your Android project. This library show up in my head as an very good example on how the property delegation functionality from kotlin can help us.

-----

## Usage on Activity

Binding

> Yes, the ```viewBinding()``` method will bind to the root view for you.

```Kotlin

class MainActivity : AppCompatActivity(R.layout.activity_main) {

private val binding : ActivityMainBinding by viewBinding()
}

```

Inflating

> You don't need to set the layout calling ```setContentView()```. It will be done automatically.
```Kotlin

class MainActivity : AppCompatActivity() {

private val binding : ActivityMainBinding by viewBinding(ViewBindingMethod.INFLATE)

}

```


## Usage on Fragment

Binding

> The ```viewBinding()``` method will track the fragment view lifecycle and destroy the ViewBinding instance for you.
```Kotlin
class MyFragment : Fragment(R.layout.fragment_layout) {

private val binding by viewBinding()
}
```

Inflating

> Using this way, it gives you the ```onCreateView```

```Kotlin
class MyFragment : Fragment() {

private val binding by viewBinding(ViewBindingMethod.INFLATE)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = binding.root
}
```

## Installation
Add the following dependency to your `build.gradle` file:
```groovy
dependencies {
implementation 'com.github.jrvansuita:view-binding:1.0.2'
}
```
> Don't forget to add the JitPack maven repository to the list of repositories: `maven { url "https://jitpack.io" }`