Android What is Difference between setVariable(BR.xyz, model) and databinding.setXYZ(model)

后端 未结 2 1214
萌比男神i
萌比男神i 2021-02-13 13:57

I was working on android data-binding and came across the scenario that we can set a model using following two ways:

 User user = new User(\"User\", \"Abc\"); //         


        
2条回答
  •  再見小時候
    2021-02-13 14:14

    Consider the case when you have a abstract class which does not share a common binding layout (except for of course the superclass ViewDataBinding which all binding layouts inherit from):

    public abstract classs EditorActivityFragment {
    

    In this class' onCreateView() you won't be able to use any generated methods to set your variable to the binding, as there's no common superclass besides the ViewDataBinding, thus you will be forced to use reflection, or you can use the convenience method setVariable():

    binding.setVariable(BR.viewModel,myViewModel);
    

    I hope that helps explain the use case for this method better.

提交回复
热议问题