加载中

Model-View-Presenter (MVP) architecture is widely used in today’s Android apps to separate the view from the presentation logic and the model by introducing a presenter. Model-View-ViewModel (MVVM) is quite similar to MVP, with the view model acting as an enhanced presenter, using a data binder to keep the view model and the view in sync. By binding the view to view model properties, the data binder can handle view updates without the need to manually set changed data back to the view (e.g. no more setText() or setVisibility() on a TextView). As with the presenter in MVP, the view model can easily be unit tested. This article gives an introduction to both the data binding library and the MVVM architectural pattern and how they work together on Android.

Model-View-Presenter(MVP),即模型-视图-表示层,架构被广泛应用于 Android 应用程序,通过引入表示层将视图与表示逻辑和模型分离。Model-View-ViewModel(MVVM),即模型-视图-视图模型,与 MVP 非常相似,视图模型充当增强的表示层,使用数据绑定器保持视图模型和视图同步。通过将视图绑定到视图模型属性上,数据绑定程序可以处理视图更新而无需手动更改数据来设置视图(例如,不用再设置控件 TextView 的setTest() 或者 setVisibility() 属性)。与 MVP 中的表示层一样,视图模型可以很容易地进行单元测试。本文介绍了数据绑定库和 MVVM 架构模式,以及它们在 Android 上协同工作方式。

Data Binding

What is data binding?

Data binding means that data from data sources is bound to data consumers. For us this usually means that data from local storage or network is bound to layouts. Also, an important feature of data binding is that data changes are automatically synchronized between sources and consumers.

What are the benefits of the data binding library?

TextView textView = (TextView) findViewById(R.id.label);
EditText editText = (EditText) findViewById(R.id.userinput);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress);
 
editText.addTextChangedListener(new TextWatcher() {
   @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
   @Override public void afterTextChanged(Editable s) { }
   @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
       model.setText(s.toString());
   }
});
 
textView.setText(model.getLabel());
progressBar.setVisibility(View.GONE);

We’ve all been writing this kind of code. Lots of findViewById() calls and later many calls to setters and listeners and so on. Even with libraries like ButterKnife it doesn’t really get better. With the data binding library, this is a thing of the past.

A binding class is created on compile time for you, which provides all views with an ID as fields. This means no more findViewById(). Actually it’s faster than manually calling findViewById() multiple times, because the data binding library creates code that traverses the view hierarchy only once.

The binding class also implements the binding logic from the layout files, so all those setters are actually called in the binding class. You don’t have to care about it anymore. In short, this means less code in your activities, fragments and view holders.

数据绑定

什么是数据绑定?


数据绑定是一种把数据绑定到用户界面元素(控件)的通用机制。通常,数据绑定会将数据从本地存储或者网络绑定到显示层,其特征是数据的改变会自动在数据源和用户界面之间同步。

数据绑定库的好处

TextView textView = (TextView) findViewById(R.id.label);
EditText editText = (EditText) findViewById(R.id.userinput);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress);
 
editText.addTextChangedListener(new TextWatcher() {
   @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
   @Override public void afterTextChanged(Editable s) { }
   @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
       model.setText(s.toString());
   }
});
 
textView.setText(model.getLabel());
progressBar.setVisibility(View.GONE);

如上述代码所示,大量的 findViewById() 调用之后,又是一大堆 setter/listener 之类的调用。 即使使用 ButterKnife 注入库也没有使情况改善。而数据绑定库就能很好地解决这个问题。

在编译时创建一个绑定类,它为所有视图提供一个 ID 字段,因此不再需要调用 findViewById() 方法。实际上,这种方式比调用 findViewById() 方法快数倍,因为数据绑定库创建代码仅需要遍历视图结构一次。

绑定类中也实现了视图文件的绑定逻辑,因此所有 setter 会在绑定类中被调用,你无须为之操心。总之,它能让你的代码变得更简洁。

How to setup data binding?

android {
   compileSdkVersion 25
   buildToolsVersion "25.0.1"
   ...
   dataBinding {
       enabled = true
   }
   ...
}

First thing to do is to add dataBinding { enabled = true } to your app’s build.gradle. This tells the build system to enable additional processing for data binding, like creating the binding classes from your layout files.

<layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <variable name="vm" type="com.example.ui.main.MainViewModel" />
    <import type="android.view.View" />
  </data>
  ...
</layout>

Next, wrap your layout’s top element in <layout> tags, so that a binding class is created for this layout. The binding class has the name of your layout xml file with Binding added at the end, e.g. ActivityMainBinding for activity_main.xml. As you can see above, namespace declarations also move to the layout tag. Then – inside the layout tag – declare the data you are binding as variables, giving them a name and type. In our case the only variable will be the view model, but more to this later. Optionally you can import classes so that you can use constants like View.VISIBLE or static methods.

如何设置数据绑定?

android {
   compileSdkVersion 25
   buildToolsVersion "25.0.1"
   ...
   dataBinding {
       enabled = true
   }
   ...
}

首先在  app 的 build.gradle 中添加 dataBinding { enabled = true }。之后构建系统会收到提示对数据绑定启用附加处理,如,从布局文件创建绑定类

<layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <variable name="vm" type="com.example.ui.main.MainViewModel" />
    <import type="android.view.View" />
  </data>
  ...
</layout>

接下来,在 <layout> 标签中包装下布局中的顶层元素,以便为此布局创建绑定类。绑定类具有和布局 xml 文件相同的名称,只是在结尾添加 Binding,例如, Activity_main.xml 的绑定类名字是 ActivityMainBinding。 如上所示,命名空间的声明也移到布局标记中。然后,在布局标记内声明将需要绑定的数据作为变量,并设置好名称和类型。示例中,唯一的变量是视图模型,但后续变量会增加。你可以选择导入类,以便能使用 View.VISIBLE 或静态方法等常量。

How to bind data?

<TextView
    android:id="@+id/my_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="@{vm.visible ? View.VISIBLE : View.GONE}">
    android:padding="@{vm.bigPadding ? @dimen/paddingBig : @dimen/paddingNormal}"
    android:text='@{vm.text ?? @string/defaultText + "Additional text."}' />

Data binding instructions on view attributes start with an @ and are enclosed by brackets. You can use any variables and imports you declared in the data section. The expressions support nearly everything you can do in code, for example arithmetic operators or string concatenation.

As you can see on the visibility attribute, the ternary if-then-else operator is also supported. A null coalescing operator ?? is provided, which returns the right operand if the left one was null. You can see this above with the text attribute. You can access resources as you would in normal layouts, therefore you can for example choose different dimension resources based on a boolean property of one of your variables, as you can see with the padding attribute.

Properties of your declared variables can be accessed via field access syntax, even if your code uses getters and setters. You can see this again in the text attribute on the slides. vm.text calls the getText() method of the view model. Finally, some small restrictions apply, e.g. no new objects can be created. However, the data binding library is still very powerful.

如何绑定数据?

<TextView
    android:id="@+id/my_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="@{vm.visible ? View.VISIBLE : View.GONE}">
    android:padding="@{vm.bigPadding ? @dimen/paddingBig : @dimen/paddingNormal}"
    android:text='@{vm.text ?? @string/defaultText + "Additional text."}' />

视图属性上的数据绑定指令以@开头,以大括号结束。你可以使用任何变量在数据段中导入你之前声明的变量。这些表达式基本支持你在代码中的所有操作,例如算术运算符或字符串连接。

Visibility 属性中还支持 if-then-else 三元运算符。还提供了合并运算符 ??,如果左边的值为空,则返回右操作数。在上述代码中,你可以像在正常布局中一样访问资源,因此你可以根据布尔变量的取值选择不同的 dimension 资源,也可以使用 padding 属性查看这些资源。

即使你在代码中使用 getters 和 setters,你所声明的变量的属性也可以用字段访问语法的形式访问。你可以在 slide 上的文本属性中看到此部分,其中 vm.text 调用视图模型的 getText() 方法。最后,一些小的限制也适用,例如,不能创建新对象,但是数据绑定库仍然非常强大

Which attributes can be bound?

android:text="@{vm.text}"
android:visibility="@{vm.visibility}"
android:paddingLeft="@{vm.padding}"
android:layout_marginBottom="@{vm.margin}"
app:adapter="@{vm.adapter}"

Actually, most properties of the standard views are already supported by the data binding library. Internally, the library looks for setters on the view type for the attribute names where you use data binding. For example, when you bind data to the text attribute, the library looks for a setText() method in your view’s class with the right parameter type (in this case String).

This also means that you can use setters, which normally do not have a corresponding layout attribute, by using data binding. For example you could use the app:adapter attribute on a recycler view in the xml layout to set the adapter via data binding.

With standard attributes, not every one of those have a corresponding setter method on the View, for example paddingLeft. In this case, the data binding library ships a custom setter, so that binding to padding attributes works out of the box. Now, what to do when no custom setters are provided by the library, for example for layout_marginBottom?

哪些属性是可以绑定的?

android:text="@{vm.text}"
android:visibility="@{vm.visibility}"
android:paddingLeft="@{vm.padding}"
android:layout_marginBottom="@{vm.margin}"
app:adapter="@{vm.adapter}"

实际上,标准视图的大多数属性已经被数据绑定库支持。在数据绑定库内部,当你使用数据绑定时,库按照视图类型查找属性名称的 setter。例如,当你把数据绑定到 text 属性时,绑定库会在视图类中使用合适的参数类型查找 setText() 方法,上述示例是 String。

当没有对应的布局属性时,你也可以使用数据绑定的 setter。例如,你可以在 xml 布局中的 recycleler 视图上使用 app:adapter 属性,以利用数据绑定设置适配器参数。

对于标准属性,不是所有的都在 View 上有对应的 setter 方法。例如,paddingLeft 情况下,数据绑定库支持自定义的 setter,以便将绑定转移到 padding 属性上。但是,遇到 layout_marginBottom 的情况,当绑定库没有提供自定义 setter 时我们要怎么处理呢?

Custom setters

@BindingAdapter("android:layout_marginBottom")
public static void setLayoutMarginBottom(View v, int bottomMargin) {
   ViewGroup.MarginLayoutParams layoutParams =
           (ViewGroup.MarginLayoutParams) v.getLayoutParams();
  
   if (layoutParams != null) {
       layoutParams.bottomMargin = bottomMargin;
   }
}

For these cases, custom setters can be written. Setters are annotated with the @BindingAdapter annotation, which takes the layout attribute name as argument, for which the binding adapter should be called. Above you see a binding adapter for layout_marginBottom.

The method must be public static void and must accept as parameters first the view type for which the binding adapter should be called and then the data to be bound with your desired type. In this example, we define a binding adapter for type View (and subtypes) with a bound type of int. Finally, implement the binding adapter. For layout_marginBottom, we need to get the layout parameters and set the bottom margin. Easy.

@BindingAdapter({"imageUrl", "placeholder"})
public static void setImageFromUrl(ImageView v, String url, int drawableId) {
   Picasso.with(v.getContext().getApplicationContext())
           .load(url)
           .placeholder(drawableId)
           .into(v);
}

It’s also possible to require multiple attributes to be set for a binding adapter to be called. To achieve this, provide your list of required attribute names to the @BindingAdapter annotation. Also, each of these attributes now have a typed parameter in the method. These BindingAdapters are only called, when all declared attributes are set.

I always like to define a binding adapter for loading images from URLs with a placeholder during loading. As you can see above, this is pretty easy with binding adapters and by using the Picasso image loading library. However, you can use any approach you want in your custom binding adapters.

自定义 Setter

@BindingAdapter("android:layout_marginBottom")
public static void setLayoutMarginBottom(View v, int bottomMargin) {
   ViewGroup.MarginLayoutParams layoutParams =
           (ViewGroup.MarginLayoutParams) v.getLayoutParams();
  
   if (layoutParams != null) {
       layoutParams.bottomMargin = bottomMargin;
   }
}

对于上述情况,自定义 setter 可以被重写。Setter 是使用 @BindingAdapter 注解来实现的,布局属性使用参数命名,使得绑定适配器被调用。上面示例提供了一个用于绑定 layout_marginBottom 的适配器。

方法必须是 public static void ,而且必须接受绑定适配器调用的首个视图类型作为参数,然后将数据强绑定到你需要的类型。在这个例子中,我们使用一个 int 类型为类型 View(子类型)定义一个绑定适配器。最后,实现绑定适配器接口。对于 layout_marginBottom,我们需要获取布局参数,并且设置底部间隔:

@BindingAdapter({"imageUrl", "placeholder"})
public static void setImageFromUrl(ImageView v, String url, int drawableId) {
   Picasso.with(v.getContext().getApplicationContext())
           .load(url)
           .placeholder(drawableId)
           .into(v);
}

也可能需要设置多种属性以绑定适配器调用。为了达到此目的,MMVM 会提供你的属性名称列表并用于 @BindingAdapter 实现注解。另外,在现有方法中,每个属性都有自己的名称。只有在所有声明的属性被设置后,这些 BindingAdapter 才会被调用。

在加载图片过程中,我想为加载图片定义一个绑定适配器来绑定 URL 与 placeHolder。如你所见,通过使用 Picasso image loading library,绑定适配器非常容易实现。你可以在自定义绑定适配器中使用任何你想要的方法。

Applying the binding in code

MyBinding binding;
 
// For Activity
binding = DataBindingUtil.setContentView(this, R.layout.layout);
// For Fragment
binding = DataBindingUtil.inflate(inflater, R.layout.layout, container, false);
// For ViewHolder
binding = DataBindingUtil.bind(view);
 
// Access the View with ID text_view
binding.textView.setText(R.string.sometext);
 
// Setting declared variables
binding.set<VariableName>(variable);

Now that we defined our bindings in the xml file and have written custom setters, how do we apply the binding in code? The data binding library does all the hard work for us, by generating a binding class. For getting an instance of the corresponding binding class for your layout, use the helper methods provided by the library. For activities use  DataBindingUtil.setContentView(), for fragments use inflate() and for view holders use bind(). As already mentioned, the binding class provides all views which have an ID defined as final fields. Also, you set the variables you declared in the layout files on the binding object.

在代码中使用绑定

MyBinding binding;
 
// For Activity
binding = DataBindingUtil.setContentView(this, R.layout.layout);
// For Fragment
binding = DataBindingUtil.inflate(inflater, R.layout.layout, container, false);
// For ViewHolder
binding = DataBindingUtil.bind(view);
 
// Access the View with ID text_view
binding.textView.setText(R.string.sometext);
 
// Setting declared variables
binding.set<VariableName>(variable);

现在我们在 xml 文件中定义了绑定,并且编写了自定义 setter,那我们如何在代码中使用绑定呢? 数据绑定库通过生成绑定类为我们完成所有的工作。要获取布局的相应绑定类的实例,就要用到库提供的辅助方法。Activity 对应使用 DataBindingUtil.setContentView(),fragment 对应使用 inflate(),视图拥有者请使用 bind()。 如前所述,绑定类为定义 final 字段的 ID 提供了所有视图。同样,您可以在绑定对象的布局文件中设置你所声明的变量。

Auto-updating the layout

One of the benefits of data binding is that the layout can be updated automatically by the library when data changes. However, the library still needs to be notified of data changes. This is accomplished by having the variables you set on the binding implement the Observable interface (don’t confuse this with the RxJava Observable).

For simple data types like int or boolean, the library already provides appropriate types which implement Observable, for example ObservableBoolean. Also, there is an ObservableField type for use with other objects, like strings.

public class MyViewModel extends BaseObservable {
   private Model model = new Model();
 
   public void setModel(Model model) {
       this.model = model;
       notifyChange();
   }
   
   public void setAmount(int amount) {
       model.setAmount(amount);
       notifyPropertyChanged(BR.amount);
   }
 
   @Bindable public String getText() { return model.getText(); }
   @Bindable public String getAmount() { return Integer.toString(model.getAmount()); }
}

In more complex situations, like with view models, a BaseObservable class exists, which provides easy methods for notifying the layout of changes. As you can see above in the setModel() method, we can then update the whole layout at once when the model changes by calling notifyChange().

When you look at setAmount(), you see that only one property of our model is changed. In this case, we do not want the whole layout to be updated, but just those parts that use this exact property. To achieve this, the corresponding getters of the property can be annotated with @Bindable. Then, a field in the BR class is generated, which can be passed into the notifyPropertyChanged() method. With this, the data binding library only updates those parts of your layout that actually depend on the changed property.

Summary

  • Declare variables in the layout files and use them to bind attributes of your views.

  • Create the binding in code and set the variables.

  • Make sure that your variable types implement Observable – for example by extending BaseObservable – so that data changes are automatically reflected by the layouts.

自动更新布局

如果使用数据绑定,在数据发生变化时,库代码可以控制布局自动更新。然而,库仍然需要获得关于数据变化的通知。如果绑定的变量实现了 Observable 接口(不要跟 RxJava 的 Observable混淆了)就能解决这个问题。

对于像 int 和 boolean 这样的简单数据类型,库已经提供了合适的实现 Observable 的类型,比如 ObservableBoolean。还有一个 ObservableField 类型用于其它对象,比如字符串。

public class MyViewModel extends BaseObservable {
   private Model model = new Model();
 
   public void setModel(Model model) {
       this.model = model;
       notifyChange();
   }
   
   public void setAmount(int amount) {
       model.setAmount(amount);
       notifyPropertyChanged(BR.amount);
   }
 
   @Bindable public String getText() { return model.getText(); }
   @Bindable public String getAmount() { return Integer.toString(model.getAmount()); }
}

在更复杂的情况下,比如视图模型,有一个 BaseObservable 类提供了工具方法在变化时通知布局。就像上面在 setModel() 方法中看到那样,我们可以在模型变化之后通过调用 notifyChange() 来更新整个布局。

再看看 setAmount(),你会看到模型中只有一个属性发生了变化。这种情况下,我们不希望更新整个布局,只更新用到了这个属性的部分。为达此目的,可以在属性对应的 getter 上添加 @Bindable 注解。然后 BR 类中会产生一个字段,用于传递给 notifyPropertyChanged() 方法。这样,绑定库可以只更新确实依赖变化属性的部分布局。

汇总

  • 在布局文件中申明变量并将之与视图中的属性绑定。

  • 在代码中创建绑定来设置变量。

  • 确保你的变量类型实现了 Observable 接口 —— 可以从 BaseObservable 继承 —— 这样数据变化时会自动反映到布局上。

Model View ViewModel architecture

Now let’s look at the ModelViewViewModel architecture and how the three components in this architectural pattern work together.

  • The view is the user interface, the layout. In Android this usually means an Activity, Fragment or ViewHolder and its corresponding inflated xml layout file.

  • The model is our business logic layer, which provides methods for interacting with data.

  • The view model acts as a middleman between view and model, by exposing the data from the model via properties and containing the UI state. Also, it defines commands which can be called on events like clicks. View models contain the presentation logic of your app.

In the MVVM architectural pattern, the view and the view model mainly interact with each other through data binding. Ideally, the view and view model should not know about each other. The bindings should be the glue between view and view model and handle most of the stuff in both directions. In Android however, they can not really be independent:

  1. you have to save and restore state, but the state is now in the view model.

  2. you need to tell your view model about lifecycle events.

  3. you might encounter situations where you need to call view methods directly.

For these cases, both the view and the view model should implement interfaces, which are then used for communication via commands, if necessary. In almost all cases, however, only an interface for the view model is needed, since the data binding library handles the interactions with the view, and custom components can be used e.g. when a context is needed.

The view model also updates the model, e.g. by adding a new element to the database or updating an existing one. It is also used to fetch data from the model. Ideally, the model should also notify the view model of changes, but this depends on the implementation.

Now, generelly speaking, the separation of view and view model makes the presentation logic easily testable and also helps with maintenance in the long run. Together with the data binding library this means less code and cleaner code.

模型、视图、视图模型(MVVM)架构

现在来看看 MVVM 架构,以及它的三个组成部分是如何一起工作的。

  • 视图是用户界面,即布局。在 Android 中通常是指 Activity、Fragment 或者 ViewHolder 以及配合它们使用的 XML 布局文件。

  • 模型就是业务逻辑层,提供方法与数据进行互动。

  • 视图模型就像是视图和模型的中间人,它既能访问模型的数据,又包含 UI 状态。它也定义了一些命令可以被事件,比如单击事件调用。视图模型包含了应用中的呈现逻辑。

在 MVVM 架构模式中,模型和视图模型主要通过数据绑定来进行互动。理想情况下,视图和视图模型不必相互了解。绑定应该是视图和视图模型之间的胶水,并且处理两个方向的大多数东西。然而,在Anroid中它们不能真实的分离:

  1. 你要保存和恢复状态,但现在状态在视图模型中。

  2. 你需要让视图模型知道生命周期事件。

  3. 你可能会遇到需要直接调用视图方法的情况。

在这些情况下,视图和视图模型应该实现接口,然后在需要的时候通过命令通信。视图模型的接口在任何情况都是需要的,因为数据绑定库会处理与视图的交互,并在上下文需要的时候使用自定义组件。

视图模型还会更新模型,比如往数据库添加新的数据,或者更新一个现有数据。它也用于从模型获取数据。理想情况下,模型也应该在变化的时候通知视图模型,但这取决于实现。

一般来说,视图和视图模型的分离会让呈现逻辑易于测试,也有助于维持长期运行。与数据绑定库一起会带来更少更简洁的代码。

Example

<layout xmlns:android="...">
  <data>
    <variable name="vm" type="pkg.MyViewModel" />
  </data>
 
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:visibility="@{vm.shouldShowText}"
      android:text="@={vm.text}" />
 
    <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:onClick="@{vm::onButtonClick}"
      android:text="@string/button"/>
  </FrameLayout>
</layout>

When you want to use MVVM architecture, your layouts should only reference one variable, the specific view model for this view, in this case MyViewModel. In the view model, you provide properties for the layout. This can be as easy as returning a String from a model object or more complex, depending on your use case.

public class MyViewModel extends BaseObservable {
   private Model model = new Model();
 
   public void setModel(Model model) {
       this.model = model;
       notifyChange();
   }
 
   public boolean shouldShowText() {
       return model.isTextRequired();
   }
 
   public void setText(String text) {
       model.setText(text);
   }
 
   public String getText() {
       return model.getText();
   }
 
   public void onButtonClick(View v) {
       // Save data
   }
}

Here we have a text property. As we have an EditText for user input, we can use two way data-binding, to also have the data binding library save the inputs back to the view model. For this, we create both a setter and a getter and bind the property to the text attribute of our EditText, but this time with an = sign before the bracket, which signals the library that we want two way data binding here.

Also, we only want to show the EditText when our model says that text input is required. For this, we provide a boolean property in our view model and bind it to the visibility attribute. For this to work, we also have to create a binding adapter, which sets the visibility to GONE when false and VISIBLE when true.

@BindingAdapter("android:visibility")
public static void setVisibility(View view, boolean visible) {
   view.setVisibility(visible ? View.VISIBLE : View.GONE);
}

示例

<layout xmlns:android="...">
  <data>
    <variable name="vm" type="pkg.MyViewModel" />
  </data>
 
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:visibility="@{vm.shouldShowText}"
      android:text="@={vm.text}" />
 
    <Button
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:onClick="@{vm::onButtonClick}"
      android:text="@string/button"/>
  </FrameLayout>
</layout>

使用 MVVM 的时候,布局只引用一个变量,即这个视图的视图模型,在这个示例中是 MyViewModel。在视图模型中,你需要提供布局所需要的属性,其简单复杂程度取决于你的用例。

public class MyViewModel extends BaseObservable {
   private Model model = new Model();
 
   public void setModel(Model model) {
       this.model = model;
       notifyChange();
   }
 
   public boolean shouldShowText() {
       return model.isTextRequired();
   }
 
   public void setText(String text) {
       model.setText(text);
   }
 
   public String getText() {
       return model.getText();
   }
 
   public void onButtonClick(View v) {
       // Save data
   }
}

这里有一个 text 属性。将 EditText 用于用户输入的时候,可以使用双向绑定,同时,数据绑定库将输入反馈回视图模型。为此,我们创建一个 setter 和 getter 并将属性绑定到 EditText 的 text 属性,这时候大括号前面的 = 号标志着我们要在这里进行双向绑定。

另外,我们只想在模型需要输入 text 的时候显示 EditText。这种情况下,我们会在视图模型中提供一个布尔属性将其与 visibility 属性绑定。为了让它工作,我们还要创建一个绑定适配器(BindingAdapter),在值为 false 的时候设置 visibility 为 GONE,在值为 true 的时候设置为 VISIBLE。

@BindingAdapter("android:visibility")
public static void setVisibility(View view, boolean visible) {
   view.setVisibility(visible ? View.VISIBLE : View.GONE);
}
返回顶部
顶部