https://github.com/fazliddinr/viewbinding-delegate
https://github.com/fazliddinr/viewbinding-delegate
viewbinding-delegate
Last synced: 13 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/fazliddinr/viewbinding-delegate
- Owner: fazliddinR
- Created: 2022-10-19T14:55:52.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-12T15:04:42.000Z (about 3 years ago)
- Last Synced: 2025-03-26T17:43:37.477Z (about 1 year ago)
- Topics: viewbinding-delegate
- Language: Kotlin
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ViewBindingDelegate
A delegate that makes it easier to work with [Android View Binding](https://d.android.com/topic/libraries/view-binding).
To enable view binding, set the viewBinding build option to true in the module-level build.gradle file, as shown in the following example:
```groovy
android {
buildFeatures {
viewBinding true
}
}
```
Please note that you need to copy the source code to work with the delegate, because it's not currently in some repository.
## Fragment sample
### The usual way -
```kotlin
class SampleFragment : Fragment() {
private var _binding: FragmentSampleBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSampleBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(binding) {
textView.text = "Some text"
button.setOnClickListener {
//some action
}
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
```
### With the delegate
The delegate considers fragment's view lifecycle, clears the reference when current view is destroyed and prevent memory leaks.
```kotlin
class ListFragment : Fragment(R.layout.fragment_sample) {
private val binding: FragmentSampleBinding by viewBindings()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(binding) {
textView.text = "Some text"
button.setOnClickListener {
//some action
}
}
}
```
## Activity sample
```kotlin
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by viewBindings()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
}
```