使用 DataBinding Library 的 MVVM 示例
MVVM 的重点是将包含逻辑的层与视图层分开。
在 Android 上,我们可以使用 DataBinding 库来帮助我们实现这一点,并使我们的大部分逻辑单元可测试,而不必担心 Android 依赖性。
在这个例子中,我将展示一个愚蠢的简单应用程序的核心组件,它执行以下操作:
- 在启动时伪造网络电话并显示加载微调器
- 使用单击计数器 TextView,消息 TextView 和用于递增计数器的按钮显示视图
- 按钮点击更新计数器,如果计数器达到某个数字,则更新计数器颜色和消息文本
让我们从视图层开始:
activity_main.xml:
如果你不熟悉 DataBinding 的工作方式,你应该花 10 分钟时间熟悉它。如你所见,通常使用 setter 更新的所有字段都绑定到 viewModel 变量上的函数。
如果你对 android:visibility 或 app:textColor 属性有疑问,请查看备注部分。
 <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="viewModel"
            type="de.walled.mvvmtest.viewmodel.ClickerViewModel"/>
    </data>
    <RelativeLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/activity_horizontal_margin"
        tools:context="de.walled.mvvmtest.view.MainActivity">
        <LinearLayout
            android:id="@+id/click_counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="60dp"
            android:visibility="@{viewModel.contentVisible ? View.VISIBLE : View.GONE}"
            android:padding="8dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/number_of_clicks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/ClickCounter"
                android:text="@{viewModel.numberOfClicks}"
                android:textAlignment="center"
                app:textColor="@{viewModel.counterColor}"
                tools:text="8"
                tools:textColor="@color/red"
            />
            <TextView
                android:id="@+id/static_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginStart="4dp"
                style="@style/ClickCounter"
                android:text="@string/label.clicks"
                app:textColor="@{viewModel.counterColor}"
                android:textAlignment="center"
                tools:textColor="@color/red"
            />
        </LinearLayout>
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/click_counter"
            android:layout_centerHorizontal="true"
            android:visibility="@{viewModel.contentVisible ? View.VISIBLE : View.GONE}"
            android:text="@{viewModel.labelText}"
            android:textAlignment="center"
            android:textSize="18sp"
            tools:text="You're bad and you should feel bad!"
        />
        <Button
            android:id="@+id/clicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/message"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"
            android:visibility="@{viewModel.contentVisible ? View.VISIBLE : View.GONE}"
            android:padding="8dp"
            android:text="@string/label.button"
            android:onClick="@{() -> viewModel.onClickIncrement()}"
        />
        <android.support.v4.widget.ContentLoadingProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="90dp"
            android:layout_centerHorizontal="true"
            style="@android:style/Widget.ProgressBar.Inverse"
            android:visibility="@{viewModel.loadingVisible ? View.VISIBLE : View.GONE}"
            android:indeterminate="true"
        />
    </RelativeLayout>
</layout>
接下来是模型层。我在这里:
- 表示应用程序状态的两个字段
- getters来读取点击次数和兴奋状态
- 增加点击次数的方法
- 恢复某些先前状态的方法(对于方向更改很重要)
此外,我在这里定义了一种兴奋状态,它取决于点击次数。稍后将使用它来更新视图上的颜色和消息。
重要的是要注意,模型中没有关于如何向用户显示状态的假设!
ClickerModel.java
import com.google.common.base.Optional;
import de.walled.mvvmtest.viewmodel.ViewState;
public class ClickerModel implements IClickerModel {
    private int numberOfClicks;
    private Excitement stateOfExcitement;
    public void incrementClicks() {
        numberOfClicks += 1;
        updateStateOfExcitement();
    }
    public int getNumberOfClicks() {
        return Optional.fromNullable(numberOfClicks).or(0);
    }
    public Excitement getStateOfExcitement() {
        return Optional.fromNullable(stateOfExcitement).or(Excitement.BOO);
    }
    public void restoreState(ViewState state) {
        numberOfClicks = state.getNumberOfClicks();
        updateStateOfExcitement();
    }
    private void updateStateOfExcitement() {
        if (numberOfClicks < 10) {
            stateOfExcitement = Excitement.BOO;
        } else if (numberOfClicks <= 20) {
            stateOfExcitement = Excitement.MEH;
        } else {
            stateOfExcitement = Excitement.WOOHOO;
        }
    }
}
接下来是 ViewModel。
这将触发模型的更改并从模型格式化数据以在视图上显示它们。请注意,我们在这里评估哪种 GUI 表示适合模型给出的状态(resolveCounterColor 和 resolveLabelText)。因此,我们可以轻松实现具有较低兴奋状态阈值的 UnderachieverClickerModel,而无需触及 viewModel 或 view 中的任何代码。
另请注意,ViewModel 不包含对视图对象的任何引用。所有属性都通过 @Bindable 注释绑定,并在 notifyChange()(表示所有属性需要更新)或 notifyPropertyChanged(BR.propertyName)(信号需要更新此信息)时更新。
ClickerViewModel.java
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.support.annotation.ColorRes;
import android.support.annotation.StringRes;
    
import com.android.databinding.library.baseAdapters.BR;
    
import de.walled.mvvmtest.R;
import de.walled.mvvmtest.api.IClickerApi;
import de.walled.mvvmtest.model.Excitement;
import de.walled.mvvmtest.model.IClickerModel;
import rx.Observable;
public class ClickerViewModel extends BaseObservable {
    private final IClickerApi api;
    boolean isLoading = false;
    private IClickerModel model;
    public ClickerViewModel(IClickerModel model, IClickerApi api) {
        this.model = model;
        this.api = api;
    }
    public void onClickIncrement() {
        model.incrementClicks();
        notifyChange();
    }
    public ViewState getViewState() {
        ViewState viewState = new ViewState();
        viewState.setNumberOfClicks(model.getNumberOfClicks());
        return viewState;
    }
    public Observable<ViewState> loadData() {
        isLoading = true;
        return api.fetchInitialState()
                .doOnNext(this::initModel)
                .doOnTerminate(() -> {
                    isLoading = false;
                    notifyPropertyChanged(BR.loadingVisible);
                    notifyPropertyChanged(BR.contentVisible);
                });
    }
    public void initFromSavedState(ViewState savedState) {
        initModel(savedState);
    }
    @Bindable
    public String getNumberOfClicks() {
        final int clicks = model.getNumberOfClicks();
        return String.valueOf(clicks);
    }
    @Bindable
    @StringRes
    public int getLabelText() {
        final Excitement stateOfExcitement = model.getStateOfExcitement();
        return resolveLabelText(stateOfExcitement);
    }
    @Bindable
    @ColorRes
    public int getCounterColor() {
        final Excitement stateOfExcitement = model.getStateOfExcitement();
        return resolveCounterColor(stateOfExcitement);
    }
    @Bindable
    public boolean isLoadingVisible() {
        return isLoading;
    }
    @Bindable
    public boolean isContentVisible() {
        return !isLoading;
    }
    private void initModel(final ViewState viewState) {
        model.restoreState(viewState);
        notifyChange();
    }
    @ColorRes
    private int resolveCounterColor(Excitement stateOfExcitement) {
        switch (stateOfExcitement) {
            case MEH:
                return R.color.yellow;
            case WOOHOO:
                return R.color.green;
            default:
                return R.color.red;
        }
    }
    @StringRes
    private int resolveLabelText(Excitement stateOfExcitement) {
        switch (stateOfExcitement) {
            case MEH:
                return R.string.label_indifferent;
            case WOOHOO:
                return R.string.label_excited;
            default:
                return R.string.label_negative;
        }
    }
}
在活动中将它们绑定在一起!
在这里,我们看到视图初始化 viewModel 及其可能需要的所有依赖项,必须从 android 上下文实例化。
在 viewModel 初始化之后,它通过 DataBindingUtil 绑定到 xml 布局(请检查’语法’部分以了解生成的类的命名)。
注意订阅是在此层上订阅的,因为我们必须处理在暂停或销毁活动时取消订阅它们以避免内存泄漏和 NPE。此处还会触发在 OrientationChanges 上持久化和重新加载 viewState
MainActivity.java
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import de.walled.mvvmtest.R;
import de.walled.mvvmtest.api.ClickerApi;
import de.walled.mvvmtest.api.IClickerApi;
import de.walled.mvvmtest.databinding.ActivityMainBinding;
import de.walled.mvvmtest.model.ClickerModel;
import de.walled.mvvmtest.viewmodel.ClickerViewModel;
import de.walled.mvvmtest.viewmodel.ViewState;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
public class MainActivity extends AppCompatActivity {
    private static final String KEY_VIEW_STATE = "state.view";
    private ClickerViewModel viewModel;
    private Subscription fakeLoader = Subscriptions.unsubscribed();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // would usually be injected but I feel Dagger would be out of scope
        final IClickerApi api = new ClickerApi();
        setupViewModel(savedInstanceState, api);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setViewModel(viewModel);
    }
    @Override
    protected void onPause() {
        fakeLoader.unsubscribe();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        fakeLoader.unsubscribe();
        super.onDestroy();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putSerializable(KEY_VIEW_STATE, viewModel.getViewState());
    }
    private void setupViewModel(Bundle savedInstance, IClickerApi api) {
        viewModel = new ClickerViewModel(new ClickerModel(), api);
        final ViewState savedState = getViewStateFromBundle(savedInstance);
        if (savedState == null) {
            fakeLoader = viewModel.loadData().subscribe();
        } else {
            viewModel.initFromSavedState(savedState);
        }
    }
    private ViewState getViewStateFromBundle(Bundle savedInstance) {
        if (savedInstance != null) {
            return (ViewState) savedInstance.getSerializable(KEY_VIEW_STATE);
        }
        return null;
    }
}
要查看所有操作,请查看此示例项目 。