https://github.com/jintin/bindingextension
Android ViewBinding extension to provide simpler usage in Activity, Fragment and ViewHolder.
https://github.com/jintin/bindingextension
activity android fragment viewbinding
Last synced: 13 days ago
JSON representation
Android ViewBinding extension to provide simpler usage in Activity, Fragment and ViewHolder.
- Host: GitHub
- URL: https://github.com/jintin/bindingextension
- Owner: Jintin
- License: apache-2.0
- Created: 2020-12-16T15:36:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-08-01T13:20:43.000Z (9 months ago)
- Last Synced: 2025-03-24T14:06:32.002Z (30 days ago)
- Topics: activity, android, fragment, viewbinding
- Language: Kotlin
- Homepage: https://devlibrary.withgoogle.com/products/android/repos/Jintin-BindingExtension
- Size: 175 KB
- Stars: 29
- Watchers: 3
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# BindingExtension
[](https://circleci.com/gh/Jintin/BindingExtension)
[](https://jitpack.io/#Jintin/BindingExtension)ViewBinding is an amazing tool for Android but it's not so fit in Android development as we still have to do some config. BindingExtension is built to provide a simpler usage.
## Install
Add [Jitpack](https://jitpack.io/) repository to your root `build.grable`:
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```Then add dependency in your module `build.gradle`:
```groovy
dependencies {
implementation 'com.github.jintin:BindingExtension:3.1.0'
}
```## Usage
First of all, BindingExtension using refelction a lot to link many things internally in order to provide simple usage.
To prevent having trouble with proguard, please remember to exclude `ViewBinding` related method in your proguard-rules file like this:```
-keepclassmembers class * implements androidx.viewbinding.ViewBinding {
public static ** inflate(...);
}
```### Activity
Extend from `BindingActivity` with your `ViewBinding` type then you can use `binding` directly after calling `super.onCreate(savedInstanceState)` and you don't have to call `setContentView` anymore:
```kotlin
class MainActivity : BindingActivity() {override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)binding.label.setText(R.string.activity_label)
}
}
```### Fragment
Extend from `BindingFragment` with your `ViewBinding` type then you can use `binding` directly after `super.onCreateView(inflater, container, savedInstanceState)` is called:
```kotlin
class MainFragment : BindingFragment() {override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)binding.button.setOnClickListener {
binding.button.setText(R.string.fragment_label)
}
}
}
```### ViewHolder
BindingExtension provide an extension function for `ViewGroup`, you can call `ViewGroup.toBinding()` in `onCreateViewHolder` to get your desire type of `ViewBinding`.
And `ViewHolder` can than access `ViewBinding` directly without further transformation.```kotlin
// inside adapter
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return MainViewHolder(parent.toBinding())
}// ViewHolder
class MainViewHolder(private val binding: AdapterMainBinding) :
RecyclerView.ViewHolder(binding.root) {fun bind(data: String) {
binding.name.text = data
}
}```
You can go to ./app module for more information.
## Contributing
Bug reports and pull requests are welcome on GitHub at [https://github.com/Jintin/BindingExtension](https://github.com/Jintin/BindingExtension).[](https://www.buymeacoffee.com/jintin)