Android MVP Examples of
simple request weather features, demonstrates how to use the Android MVP
Results preview
Ready
MVP Process
Description:
Step 1: UI View implemented method, reference Presenter
Step 2: Model Presenter call, go Model specific logical
steps. 3: Model logic implementations, Presenter callback method
steps. 4: View Presenter callback, i.e. back to the UI, the callback method View
gradle document
. 1
the compile 'com.loopj.android:android-async-http:1.4.9'
instructions: requesting a network using the async-http
Directory Structure
MVP of the V
MainView.java
/ **
* the Created by WuXiaolong ON 2015/9/23.
* Business processing method which requires
* /
public interface MainView {
void showData(MainModelBean mainModelBean);
void showProgress();
void hideProgress();
}
MainActivity
/ **
* the Created by WuXiaolong ON 2015/9/23.
* View in the method implemented by the Activity / Fragment, contains a reference to a Presenter
* /
public class MainActivity extends AppCompatActivity implements MainView {
private ProgressBar mProgressBar;
private TextView text;
private MainPresenter mMainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
text = (TextView) findViewById(R.id.text);
mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
mMainPresenter = new MainPresenter(this);
//制造延迟效果
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mMainPresenter.loadData();
}
}, 2000);
}
@Override
protected void onDestroy() {
mMainPresenter.detachView();
super.onDestroy();
}
@Override
public void showData(MainModelBean mainModelBean) {
String showData = getResources().getString(R.string.city) + mainModelBean.getCity()
+ getResources().getString(R.string.wd) + mainModelBean.getWd()
+ getResources().getString(R.string.ws) + mainModelBean.getWs()
+ getResources().getString(R.string.time) + mainModelBean.getTime();
text.setText(showData);
}
@Override
public void showProgress() {
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void hideProgress() {
mProgressBar.setVisibility(View.GONE);
}
}
MVP of the P
MainPresenter.java
/ **
* the Created by WuXiaolong ON 2015/9/23.
* Bridge View and Model, which retrieves data from the Model layer, returned to the View layer
* /
public class MainPresenter implements Presenter<MainView>, IMainPresenter {
private MainView mMainView;
private MainModel mMainModel;
public MainPresenter(MainView view) {
attachView(view);
mMainModel = new MainModel(this);
}
@Override
public void attachView(MainView view) {
this.mMainView = view;
}
@Override
public void detachView() {
this.mMainView = null;
}
public void loadData() {
mMainView.showProgress();
mMainModel.loadData();
}
@Override
public void loadDataSuccess(MainModelBean mainModelBean) {
mMainView.showData(mainModelBean);
mMainView.hideProgress();
}
@Override
public void loadDataFailure() {
mMainView.hideProgress();
}
}
Presenter
public interface Presenter<V> {
void attachView(V view);
void detachView();
}
IMainPresenter
/ **
* the Created by WuXiaolong ON 2015/9/23.
* This interface function is connected Model
* /
public interface IMainPresenter {
void loadDataSuccess(MainModelBean mainModelBean);
void loadDataFailure();
}
MVP of M
MainModel
/ **
* the Created by WuXiaolong ON 2015/9/23.
* Specific processing operations, including those responsible for storing, retrieving, manipulating data
* /
public class MainModel {
IMainPresenter mIMainPresenter;
public MainModel(IMainPresenter iMainPresenter) {
this.mIMainPresenter = iMainPresenter;
}
public void loadData() {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get("http://www.weather.com.cn/adat/sk/101010100.html", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
try {
MainModelBean mainModelBean = new MainModelBean();
JSONObject weatherinfo = response.getJSONObject("weatherinfo");
mainModelBean.setCity(weatherinfo.getString("city"));
mainModelBean.setWd(weatherinfo.getString("WD"));
mainModelBean.setWs(weatherinfo.getString("WS"));
mainModelBean.setTime(weatherinfo.getString("time"));
mIMainPresenter.loadDataSuccess(mainModelBean);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
mIMainPresenter.loadDataFailure();
}
});
}
}
MainModelBean
public class MainModelBean {
private String city;
private String wd;
private String ws;
private String time;
//此处省略get和set方法
}
Source address
https://github.com/WuXiaolong/AndroidMVPSample
Summary
MVC pattern
view (View): the user interface.
A controller (Controller): business logic
model (Model): data storage
View Controller transfer instruction to
the Controller complete the business logic required to change the state Model
Model send new data to the View, the user feedback
MVP pattern
when using the MVP, and Fragment into the Activity of the MVC pattern layer View, Presenter Controller layer corresponding to the MVC model, processing business logic. Each has a corresponding presenter Activity data is processed and then obtain model.
MVVM pattern
will be renamed Presenter ViewModel, substantially identical with the MVP pattern. The only difference is that it uses two-way binding (data-binding): View the changes are automatically reflected in the ViewModel, and vice versa.