Search code examples
androidkotlinandroid-databindingandroid-viewbinding

How to use <data> <variable> tags in .xml using ViewBinding?


I am trying to go from DataBinding to ViewBinding. And stumbled upon a problem, what to do with .xml files that use data and variable tags. I am using them to interact with the ViewModel object.

<data>

    <import type="android.view.View"/>

    <variable
        name="viewModel"
        type="app.myapp.android.main.viewmodel.MainViewModel" />
</data>

I would be grateful for any help!


Solution

  • You can't use viewmodel in xml ,if you are moving from data binding to view binding. You can only use viewbinding to eliminate findViewById().If you want viewmodel in recycleview adapter or somewhere you can send viewmodel instance from activity to adapter through adapter constructor.