看到一篇干货,里面是作者给工作一年的开发经验:
里面讲到的第四点 "从 MVC 到 MVP",看作者开源项目之前先来学习 MVP
1. 调用网络接口获取天气
2. 简单登陆界面
3.listview
4.SpareArray 存储数据
分析网络接口获取天气
Model 层:
Model 层负责网络获取数据
- public interface IWeatherModel {
- void loadWeather(String cityNum, OnWeatherListener onWeatherListener);
- }
实现类:
通过封装的 Volley 请求向接口请求数据,并通过 OnWeatherListener 回调
- public class WeatherModel implements IWeatherModel {@Overridepublic void loadWeather(String cityNum, final OnWeatherListener listener) {
- VolleyRequest.newInstance().newGsonRequest("http://www.weather.com.cn/data/sk/" + cityNum + ".html", Weather.class, new Response.Listener < Weather > () {@Overridepublic void onResponse(Weather weather) {
- listener.onSuccess(weather);
- }
- },
- new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError volleyError) {
- listener.onError();
- }
- });
- }
- }
天气数据获取的回调接口,将数据从 Model 层回调到 Presenter 层 (p 层负责将信息加载到 Activity 上,即 View 层上)
- public interface OnWeatherListener {
- void onSuccess(Weather weather);
- void onError();
- }
Presenter 层:
获取 model 数据,更改 UI
- public interface IWeatherPresenter {
- void getWeather(String cityNum);
- }
Presenter: 作为 View 与 Model 交互的中间纽带,显示天气信息
- public class WeatherPresenter implements IWeatherPresenter,
- OnWeatherListener {
- private IWeatherView view;
- private IWeatherModel model;
- public WeatherPresenter(IWeatherView view) {
- this.view = view;
- model = new WeatherModel();
- }@Overridepublic void getWeather(String cityNum) {
- if (view != null) {
- view.showLoading();
- model.loadWeather(cityNum, this);
- }
- }@Overridepublic void onError() {
- if (view != null) {
- view.hideLoading();
- view.showError();
- }
- }@Overridepublic void onSuccess(Weather weather) {
- if (view != null) {
- view.hideLoading();
- view.setWeatherInfo(weather);
- }
- }
- }
View 层:
- public interface IWeatherView {
- void showLoading();
- void hideLoading();
- void showError();
- void setWeatherInfo(Weather weather);
- }
View 实现类:
将 Activity 作为 View 层,通过 Presenter 层对控件的各种动作进行控制,达到分离的目的
- public class WeatherActivity extends AppCompatActivity implements IWeatherView,
- View.OnClickListener {
- 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;
- private Button go;
- private IWeatherPresenter weatherPresenter;@Overrideprotected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_weather);
- VolleyRequest.buildRequestQueue(this);
- init();
- }
- private void init() {
- cityNOInput = (EditText) findViewById(R.id.et_city_no);
- city = (TextView) findViewById(R.id.tv_city);
- cityNO = (TextView) findViewById(R.id.tv_city_no);
- temp = (TextView) findViewById(R.id.tv_temp);
- wd = (TextView) findViewById(R.id.tv_WD);
- ws = (TextView) findViewById(R.id.tv_WS);
- sd = (TextView) findViewById(R.id.tv_SD);
- wse = (TextView) findViewById(R.id.tv_WSE);
- time = (TextView) findViewById(R.id.tv_time);
- njd = (TextView) findViewById(R.id.tv_njd);
- go = (Button) findViewById(R.id.btn_go);
- go.setOnClickListener(this);
- weatherPresenter = new WeatherPresenter(this);
- loadingDialog = new ProgressDialog(this);
- loadingDialog.setTitle("加载天气中...");
- }@Overridepublic void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_go:
- weatherPresenter.getWeather(cityNOInput.getText().toString().trim());
- break;
- }
- }@Overridepublic void hideLoading() {
- loadingDialog.hide();
- }@Overridepublic void showLoading() {
- loadingDialog.show();
- }@Overridepublic void showError() {
- Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
- }@Overridepublic void setWeatherInfo(Weather weather) {
- WeatherInfo info = weather.getWeatherinfo();
- city.setText(info.getCity());
- cityNO.setText(info.getCityid());
- temp.setText(info.getTemp());
- wd.setText(info.getWD());
- ws.setText(info.getWS());
- sd.setText(info.getSD());
- wse.setText(info.getWS());
- time.setText(info.getTemp());
- njd.setText(info.getNjd());
- }
- }
总结:Model 处理各种后台任务和网络请求等,Presenter 层对 Model 获取到的数据加载到 VIew 层,View 层提供一系列 UI 操作让 Presenter 层来控制,从而实现 VIew 和 Model 的分离。
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: