@linux1s1s
2016-04-26T03:38:45.000000Z
字数 4883
阅读 2602
Android_Architecture 2016-04
在Android开发中,比较流行的开发框架模式采用的是MVC框架模式,采用MVC模式的好处是便于UI界面部分的显示和业务逻辑,数据处理分开。
那么Android项目中哪些代码来充当M,V,C角色呢?
Controller层:在Android中,Activity处理用户交互问题。
所以我们来重新温习一下MVC的交互图

Talk is cheap,show me the code.(废话少说,直接上代码)
Controller层(主要是Activity)
import android.app.Dialog;import android.app.ProgressDialog;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import com.xjp.androidmvcdemo.R;import com.xjp.androidmvcdemo.entity.Weather;import com.xjp.androidmvcdemo.entity.WeatherInfo;import com.xjp.androidmvcdemo.model.OnWeatherListener;import com.xjp.androidmvcdemo.model.WeatherModel;import com.xjp.androidmvcdemo.model.WeatherModelImpl;public class MainActivity extends ActionBarActivity implements OnWeatherListener, View.OnClickListener {private WeatherModel weatherModel;private Dialog loadingDialog;private EditText cityNOInput;private TextView city;private TextView cityNO;private TextView temp;private TextView wd;private TextView ws;private TextView sd;private TextView wse;private TextView time;private TextView njd;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);weatherModel = new WeatherModelImpl();initView();}/*** 初始化View*/private void initView() {cityNOInput = findView(R.id.et_city_no);city = findView(R.id.tv_city);cityNO = findView(R.id.tv_city_no);temp = findView(R.id.tv_temp);wd = findView(R.id.tv_WD);ws = findView(R.id.tv_WS);sd = findView(R.id.tv_SD);wse = findView(R.id.tv_WSE);time = findView(R.id.tv_time);njd = findView(R.id.tv_njd);findView(R.id.btn_go).setOnClickListener(this);loadingDialog = new ProgressDialog(this);loadingDialog.setTitle(加载天气中...);}/*** 显示结果** @param weather*/public void displayResult(Weather weather) {WeatherInfo weatherInfo = weather.getWeatherinfo();city.setText(weatherInfo.getCity());cityNO.setText(weatherInfo.getCityid());temp.setText(weatherInfo.getTemp());wd.setText(weatherInfo.getWD());ws.setText(weatherInfo.getWS());sd.setText(weatherInfo.getSD());wse.setText(weatherInfo.getWSE());time.setText(weatherInfo.getTime());njd.setText(weatherInfo.getNjd());}/*** 隐藏进度对话框*/public void hideLoadingDialog() {loadingDialog.dismiss();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_go:loadingDialog.show();weatherModel.getWeather(cityNOInput.getText().toString().trim(), this);break;}}@Overridepublic void onSuccess(Weather weather) {hideLoadingDialog();displayResult(weather);}@Overridepublic void onError() {hideLoadingDialog();Toast.makeText(this, 获取天气信息失败, Toast.LENGTH_SHORT).show();}private <t extends="" view=""> T findView(int id) {return (T) findViewById(id);}}
其中的接口OnWeatherListener是Module的回调,也就是上面交互图中的Module指向View的部分,通知View更新界面,这个接口可以减轻耦合度,方便单元测试Mock数据。
public interface OnWeatherListener {public void onSuccess(Weather weather);public void onError();}
从上面代码可以看到,Activity持有了WeatherModel模型的对象,当用户有点击Button交互的时候,Activity作为Controller控制层读取View视图层EditTextView的数据,然后向Model模型发起数据请求,也就是调用WeatherModel对象的方法 getWeathre()方法。当Model模型处理数据结束后,通过接口OnWeatherListener通知View视图层数据处理完毕,View视图层该更新界面UI了。然后View视图层调用displayResult()方法更新UI。至此,整个MVC框架流程就在Activity中体现出来了。
Module层(抽象数据)
public class WeatherModelImpl implements WeatherModel {@Overridepublic void getWeather(String cityNumber, final OnWeatherListener listener) {/*数据层操作*/VolleyRequest.newInstance().newGsonRequest(http://www.weather.com.cn/data/sk/ + cityNumber + .html,Weather.class, new Response.Listener<weather>() {@Overridepublic void onResponse(Weather weather) {if (weather != null) {listener.onSuccess(weather);} else {listener.onError();}}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {listener.onError();}});}}
Module层同样为了减轻耦合性,抽取接口WeatherModel这样可以很容易的更换Remote/Local获取数据的方式,这个接口具体如下.
public interface WeatherModel {void getWeather(String cityNumber, OnWeatherListener listener);}
以上代码看出,这里设计了一个WeatherModel模型接口,然后实现了接口WeatherModelImpl类。controller控制器activity调用WeatherModelImpl类中的方法发起网络请求,然后通过实现OnWeatherListener接口来获得网络请求的结果通知View视图层更新UI 。至此,Activity就将View视图显示和Model模型数据处理隔离开了。activity担当contronller完成了model和view之间的协调作用。
看完了上面的Code,我们得出来一个结论和一个疑问。
附:
MVX Android设计架构浅析-MVC
MVX Android设计架构浅析-MVP
MVX Android设计架构浅析-MVVM
