Back to Mavericks

View Binding

docs/view-binding.md

3.1.01.0 KB
Original Source

View Binding

View Binding is Google's replacement for findViewById(), Kotlin synthetic accessors, and Butterknife. Although not strictly Mavericks related, our sample apps use custom delegates to easily use View Binding with a single line of code. Add FragmentViewBindingDelegate.kt to your project then you can use it like this:

kotlin
class CounterFragment : BaseFragment(R.layout.counter_fragment) {
  private val binding: CounterFragmentBinding by viewBinding()
  private val viewModel: CounterViewModel by fragmentViewModel()

  override fun invalidate() = withState(viewModel) { state ->
    binding.textView.text = "Count: ${state.count}"
  }
}

The by viewBinding() delegate feels very similar to the Mavericks View Model delegate syntax so the two play well together.