Android 官方示例:android-architecture 学习笔记(四)之todo-databinding

项目地址:https://github.com/googlesamples/android-architecture/tree/todo-databinding/

项目结构

本项目基于todo-mvp(后文所说”对比之前的项目”,即是说的它),并使用了Data Binding library 来将数据绑定并显示到UI元素上,如图
Android 官方示例:android-architecture 学习笔记(四)之todo-databinding_第1张图片
本项目,不遵循一个严格的Model-View-ViewModel或Model-View-Presenter模式,因为它同时使用ViewModel和Presenter

模块分析

added it task模块

包addedittask,下的AddEditTaskFragment,看下它的主要代码:

public class AddEditTaskFragment extends Fragment implements AddEditTaskContract.View {

    private AddEditTaskContract.Presenter mPresenter;

    private AddtaskFragBinding mViewDataBinding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);
        if (mViewDataBinding == null) {
            mViewDataBinding = AddtaskFragBinding.inflate(inflater, container, false);
        }
        mTitle = mViewDataBinding.addTaskTitle;
        mDescription = mViewDataBinding.addTaskDescription;

        setHasOptionsMenu(true);
        // Fragment is retained simply to persist the edits after rotation.
        setRetainInstance(true);
        return mViewDataBinding.getRoot();
    }
}

AddEditTaskFragment实现了MVP中V的接口。在onCreateView()中,使用了数据绑定框架生成binding class ——— AddtaskFragBinding。根据数据绑定框架定义,应该一个对应的布局名为addtask_frag.xml。查看res/layout下果然有该xml。

statistics模块

查看包结构目录,发现多了一个StatisticsViewModel类。

public class StatisticsViewModel implements StatisticsContract.View

它实现了MVP中的V的接口。再看下StatisticsFragment的代码:

public class StatisticsFragment extends Fragment {

    private StatisticsPresenter mPresenter;

    private StatisticsFragBinding mViewDataBinding;

    private StatisticsViewModel mStatisticsViewModel;

    public static StatisticsFragment newInstance() {
        return new StatisticsFragment();
    }

    public void setPresenter(@NonNull StatisticsPresenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mViewDataBinding = DataBindingUtil.inflate(
                inflater, R.layout.statistics_frag, container, false);
        View root = mViewDataBinding.getRoot();
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewDataBinding.setStats(mStatisticsViewModel);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    public void setViewModel(StatisticsViewModel statisticsViewModel) {
        mStatisticsViewModel = statisticsViewModel;
    }

    public boolean isActive() {
        return isAdded();
    }
}

这个类的代码不长,就全贴了。StatisticsPresenter就不说了。成员属性还有StatisticsFragBinding和StatisticsViewModel。生命周期函数onCreateView中,初始化了mViewDataBinding;onActivityCreated中,绑定了mStatisticsViewModel。
再看下statistics_frag.xml主要内容:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.view.View" />
        <variable
            name="stats" type="com.example.android.architecture.blueprints.todoapp.statistics.StatisticsViewModel" />
    data>
    <LinearLayout>
     ....
    LinearLayout>
layout>

对于variable-stats,数据绑定框架会生成一组getter和setter,名为SetStats(StatisticsViewModel model)和 StatisticsViewModel getStats()。

总结

本项目基本就是这两种架构方式。更多的学习成本在Data Binding框架上,为此专门翻译了一下官方的 Data Binding Library 文档

你可能感兴趣的:(Android,开源项目,Android,架构框架,Android)