Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine Architecture Components with data binding on android?

I have developed app base on android data binding library: https://developer.android.com/topic/libraries/data-binding/index.html

class SignInViewModel extends BaseObservable {

    @Bindable
    public String getLogin() {
        return login;
    }

    @Bindable
    public String getPassword() {
        return password;
    }
}

and now I want to use ViewModelProviders from new library: https://developer.android.com/topic/libraries/architecture/guide.html

SignInViewModel signInViewModel = ViewModelProviders.of(this).get(SignInViewModel.class);

How it combine? any idea? or should be combined these two libraries?

Edit

I change to:

class SignInViewModel extends ViewModel {
   public ObservableField<String> login = new ObservableField<>("");

    public ObservableField<String> password = new ObservableField<>("");
}

and now compiles, but question is: is it right way?

like image 348
LunaVulpo Avatar asked May 19 '17 19:05

LunaVulpo


People also ask

Can I use both data binding and view binding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

What is 2 way data binding in Android?

Two-way data binding is nothing but updating the data source if there are any changes in the layout and vice versa. Two-way data binding is not applicable for all the views in Android. For example, using two-way data binding in EditText makes sense because users can update the data in the view.

What is data binding in Mvvm Android?

Up until now, we've used Data Binding to update the View from the ViewModel. LiveData is a handy data holder that acts as a container over the data to be passed. The best thing about LiveData is that it is lifecycle aware. So if you are in the background, the UI won't try to update.

How do I enable data binding in build gradle?

Activate the usage of data binding. Open your app/build. gradle file and activate the usage of data binding. apply plugin: 'com.


1 Answers

It's a known incompatibility. You can't extend BaseObservable and AndroidViewModel at the same time, so you can't use @Bindable making two-way data binding impossible*.

This will be fixed after arch components 1.0 final (on the data binding side).

*Edit: You can make your own ObservableViewModel: https://gist.github.com/JoseAlcerreca/4b66f9953d50b483d80e6b9ad7172685

like image 178
Jose Alcérreca Avatar answered Oct 24 '22 10:10

Jose Alcérreca