0leg
0leg

Reputation: 14134

ViewModel and Data Binding

Android has recently introduced Architecture Components and in particular a ViewModel, which is

designed to store and manage UI-related data so that the data survives configuration changes such as screen rotations

In example provided by Google, ViewModel is used like this:

public class MyActivity extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
        model.getUsers().observe(this, users -> {
            // update UI
        });
    }
}

Question: How does ViewModel suppose to correlate with the Data Binding?

I mean, in case of data binding there is going to be a binding which provides data for the UI.

Is it going to look like this:

...
model.getUsers().observe(this, users -> {
  // update binding, that will auto-update the UI?
});
...

Upvotes: 9

Views: 9446

Answers (2)

Rishabh Jain
Rishabh Jain

Reputation: 81

Your layout_name.xml file would look like

<layout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools">

   <data>
       <import type="android.view.View"/>
       <variable
           name="model"
           type="com.yourpackage.ViewModelName"/>
   </data>

   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@color/white"
       android:visibility="@{model.someVariable == true ? View.VISIBLE : View.GONE}">

   </RelativeLayout>
</layout>

Your Activity class would look like

public class YourActivityName extends BaseActivity
{
    private ViewModelName viewModelVariable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewModelName viewModelVariable = new ViewModelName(); 
        ViewDataBinding viewDataBinding = DataBindingUtil.setContentView(this, R.layout.layout_name);
        viewDataBinding.setVariable(BR.model, viewModelVariable);

    }
}

ViewModel class would look like

public class ViewModelName extends BaseObservable{
    //Logic and variables for view model
    public boolean someVariable;
}

Upvotes: -1

dzikovskyy
dzikovskyy

Reputation: 5087

You can declare a variable of your viewmodel type in your layout xml file. In your viewmodel class implement public methods through which data will be bind to ui.

Then you only need to set view model into binding in onCreate. When you set view model instance in databinding, the data which is already loaded in viewmodel will be set to recreated layout.

In case there is a recycler view in your layout you can implement some public method like initRecyclerView() in your view model class and call it in onCreate() after setting view model in binding or adapter can be set from view model through databinding as well.

Upvotes: 2

Related Questions