[关闭]
@linux1s1s 2016-04-26T03:38:45.000000Z 字数 4883 阅读 2070

MVX Android设计架构浅析-MVC

Android_Architecture 2016-04


简述

在Android开发中,比较流行的开发框架模式采用的是MVC框架模式,采用MVC模式的好处是便于UI界面部分的显示和业务逻辑,数据处理分开。

那么Android项目中哪些代码来充当M,V,C角色呢?

简化的交互图

所以我们来重新温习一下MVC的交互图
此处输入图片的描述

代码

Talk is cheap,show me the code.(废话少说,直接上代码)

Controller层(主要是Activity)

  1. import android.app.Dialog;
  2. import android.app.ProgressDialog;
  3. import android.os.Bundle;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import com.xjp.androidmvcdemo.R;
  10. import com.xjp.androidmvcdemo.entity.Weather;
  11. import com.xjp.androidmvcdemo.entity.WeatherInfo;
  12. import com.xjp.androidmvcdemo.model.OnWeatherListener;
  13. import com.xjp.androidmvcdemo.model.WeatherModel;
  14. import com.xjp.androidmvcdemo.model.WeatherModelImpl;
  15. public class MainActivity extends ActionBarActivity implements OnWeatherListener, View.OnClickListener {
  16. private WeatherModel weatherModel;
  17. private Dialog loadingDialog;
  18. private EditText cityNOInput;
  19. private TextView city;
  20. private TextView cityNO;
  21. private TextView temp;
  22. private TextView wd;
  23. private TextView ws;
  24. private TextView sd;
  25. private TextView wse;
  26. private TextView time;
  27. private TextView njd;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. weatherModel = new WeatherModelImpl();
  33. initView();
  34. }
  35. /**
  36. * 初始化View
  37. */
  38. private void initView() {
  39. cityNOInput = findView(R.id.et_city_no);
  40. city = findView(R.id.tv_city);
  41. cityNO = findView(R.id.tv_city_no);
  42. temp = findView(R.id.tv_temp);
  43. wd = findView(R.id.tv_WD);
  44. ws = findView(R.id.tv_WS);
  45. sd = findView(R.id.tv_SD);
  46. wse = findView(R.id.tv_WSE);
  47. time = findView(R.id.tv_time);
  48. njd = findView(R.id.tv_njd);
  49. findView(R.id.btn_go).setOnClickListener(this);
  50. loadingDialog = new ProgressDialog(this);
  51. loadingDialog.setTitle(加载天气中...);
  52. }
  53. /**
  54. * 显示结果
  55. *
  56. * @param weather
  57. */
  58. public void displayResult(Weather weather) {
  59. WeatherInfo weatherInfo = weather.getWeatherinfo();
  60. city.setText(weatherInfo.getCity());
  61. cityNO.setText(weatherInfo.getCityid());
  62. temp.setText(weatherInfo.getTemp());
  63. wd.setText(weatherInfo.getWD());
  64. ws.setText(weatherInfo.getWS());
  65. sd.setText(weatherInfo.getSD());
  66. wse.setText(weatherInfo.getWSE());
  67. time.setText(weatherInfo.getTime());
  68. njd.setText(weatherInfo.getNjd());
  69. }
  70. /**
  71. * 隐藏进度对话框
  72. */
  73. public void hideLoadingDialog() {
  74. loadingDialog.dismiss();
  75. }
  76. @Override
  77. public void onClick(View v) {
  78. switch (v.getId()) {
  79. case R.id.btn_go:
  80. loadingDialog.show();
  81. weatherModel.getWeather(cityNOInput.getText().toString().trim(), this);
  82. break;
  83. }
  84. }
  85. @Override
  86. public void onSuccess(Weather weather) {
  87. hideLoadingDialog();
  88. displayResult(weather);
  89. }
  90. @Override
  91. public void onError() {
  92. hideLoadingDialog();
  93. Toast.makeText(this, 获取天气信息失败, Toast.LENGTH_SHORT).show();
  94. }
  95. private <t extends="" view=""> T findView(int id) {
  96. return (T) findViewById(id);
  97. }
  98. }

其中的接口OnWeatherListener是Module的回调,也就是上面交互图中的Module指向View的部分,通知View更新界面,这个接口可以减轻耦合度,方便单元测试Mock数据。

  1. public interface OnWeatherListener {
  2. public void onSuccess(Weather weather);
  3. public void onError();
  4. }

从上面代码可以看到,Activity持有了WeatherModel模型的对象,当用户有点击Button交互的时候,Activity作为Controller控制层读取View视图层EditTextView的数据,然后向Model模型发起数据请求,也就是调用WeatherModel对象的方法 getWeathre()方法。当Model模型处理数据结束后,通过接口OnWeatherListener通知View视图层数据处理完毕,View视图层该更新界面UI了。然后View视图层调用displayResult()方法更新UI。至此,整个MVC框架流程就在Activity中体现出来了。

Module层(抽象数据)

  1. public class WeatherModelImpl implements WeatherModel {
  2. @Override
  3. public void getWeather(String cityNumber, final OnWeatherListener listener) {
  4. /*数据层操作*/
  5. VolleyRequest.newInstance().newGsonRequest(http://www.weather.com.cn/data/sk/ + cityNumber + .html,
  6. Weather.class, new Response.Listener<weather>() {
  7. @Override
  8. public void onResponse(Weather weather) {
  9. if (weather != null) {
  10. listener.onSuccess(weather);
  11. } else {
  12. listener.onError();
  13. }
  14. }
  15. }, new Response.ErrorListener() {
  16. @Override
  17. public void onErrorResponse(VolleyError error) {
  18. listener.onError();
  19. }
  20. });
  21. }
  22. }

Module层同样为了减轻耦合性,抽取接口WeatherModel这样可以很容易的更换Remote/Local获取数据的方式,这个接口具体如下.

  1. public interface WeatherModel {
  2. void getWeather(String cityNumber, OnWeatherListener listener);
  3. }

看完了上面的Code,我们得出来一个结论和一个疑问。

附:
MVX Android设计架构浅析-MVC
MVX Android设计架构浅析-MVP
MVX Android设计架构浅析-MVVM

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注