Android架构模式——MVVM

复习mvvm前,先复习下DataBinding:
什么是DataBinding? DataBinding是谷歌官方发布的一个数据绑定的框架,它需要Android 2.1和Gradle1.5.0以上版本环境下才能运行,它减少了大量findviewbyid代码,降低了耦合,使项目更清晰简洁!

1.Model:模型层,负责处理数据的加载或者存储
2.View:视图层,负责界面数据的展示,与用户进行交互
3.ViewModel:处理业务逻辑,实现view层和数据的绑定

先将之前的mvp架构的项目改造成mvvm架构的!!
Android架构模式——MVVM_第1张图片
1.建立MvvmViewModel

public class MvvmViewModel {
    public final ObservableField result=new ObservableField();

    public  void getParm(String url,String id){
        // 创建OkHttpClient.Builder对象
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder() ;
        // 设置拦截器
        builder.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                // 设置Header
                Request newRequest = chain.request().newBuilder()
                        .removeHeader("User-Agent")
                        .addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36")
                        .build() ;
                return chain.proceed(newRequest);
            }
        }) ;
        OkHttpClient client = builder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MyService service = retrofit.create(MyService.class);

        service.getInfor(id).enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                InforBean inforBean = response.body();
                if (inforBean.getCode().equals("200")) {
                    Log.e("msg", "成功");
                    result.set(inforBean.getName()+":"+inforBean.getAge());

                } else {
                    Log.e("msg", "失败");
                    result.set("失败");
                }

            }
            @Override
            public void onFailure(Call call, Throwable t) {
                t.getStackTrace();
                Log.e("msg", "连接失败");
                result.set("连接失败");
            }
        });

    }
}

2.修改main_activity.xml


    
        
    

    
    

3.建立MvvmActivity

public class MvvmActivity extends AppCompatActivity implements View.OnClickListener{
    private MvvmViewModel mvm;
    private Button bt;
    private String url="xxxxx";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      MainAvtivityBinding mab= DataBindingUtil.setContentView(this,R.layout.main_avtivity);
      mvm=new MvvmViewModel();
      mab.setViewModel(mvm);
      initView();
      initData();
    }
    private void initData(){
        mvm.getParm(url,"1");
    }
    private void initView(){
        bt=findViewById(R.id.bt);
        bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt:
                mvm.getParm(url,"2");
                break;
            default:
                break;
        }
    }
}

至此就实现了mvvm架构模式,对比之前的mvc和mvp,代码量更少,创建的类也是最少的。

上一篇:Android架构模式——mvp

下一篇:Android之hook框架——Legend学习

你可能感兴趣的:(android)