如何在android上将架构组件与数据绑定相结合? [英] how to combine Architecture Components with data binding on android?

查看:24
本文介绍了如何在android上将架构组件与数据绑定相结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了基于 android 数据绑定库的应用程序:https://developer.android.com/topic/libraries/data-binding/index.html

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;
    }
}

现在我想使用新库中的 ViewModelProviders:https://developer.android.com/topic/libraries/architecture/guide.html

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?

编辑

我改为:

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?

推荐答案

这是已知的不兼容性.不能同时扩展BaseObservableAndroidViewModel,所以不能用@Bindable做双向数据绑定不可能*.

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*.

这将在 arch components 1.0 final 之后修复(在数据绑定方面).

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

*您可以制作自己的 ObservableViewModel:https://gist.github.com/JoseAlcerreca/4b66f9953d50b483d80e6b9ad7172685

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

这篇关于如何在android上将架构组件与数据绑定相结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆