-
Notifications
You must be signed in to change notification settings - Fork 4
Home
Hi. You probably already know why you're here, so let's not waste any more time.
Here's a quick guide how to use Androidbound, the viewbinding library for Android that enables you to use MVVM.
-
Setup the Activity and ViewModel.
If you extend normal (non-Binding) Activities:
IAndroidLogger logger = new DetailedAndroidLogger(LOGGING_TAG, LOGGING_LEVEL); ViewBinder viewBinder = new ViewBinder(this, logger, null); ViewModel viewModel = new MainActivityViewModel(this, logger); View view = viewBinder.inflate(this, viewModel, R.layout.activity_main, null); setContentView(view);
If you extend a Binding Activity, like BindingAppCompatActivity:
IAndroidLogger logger = new DetailedAndroidLogger(LOGGING_TAG, LOGGING_LEVEL); ViewBinder viewBinder = new ViewBinder(this, logger, null); ViewModel viewModel = new MainBindableActivityViewModel(this, logger); setViewBinder(viewBinder); setBoundData(viewModel); setContentView(R.layout.activity_bindable_main);
-
Add binding to you widget in the layout file, e.g.
<TextView android:id="@+id/activity_title" android:layout_width="wrap_content" android:layout_height="wrap_content" binding="{ Text @= MainActivityTitle }; { TextColor @= MainActivityTitleColor }"/>
-
In your ViewModel, either add the property called
MainActivityTitle
, like this:public String MainActivityTitle = "Title";
or add the accessor method starting with get:
public String getMainActivityTitle() { return "Title"; }
When the view is initialized, it will automatically pass this value to the TextView. If you want to refresh it later, you should call
raisePropertyChanged("MainActivityTitle")
and the TextView will receive the new value of the bound property (MainActivityTitle
) or it will call the bound method again (getMainActivityTitle()
). For example, this is what a setter would like for the property:public void setMainActivityTitle(String title) { MainActivityTitle = title; raisePropertyChanged("MainActivityTitle"); }
-
When you don't need the ViewModel any more, simply call
ViewModel.dispose()
which should release all the references and unsubscribe from all the subscriptions.