MVP+Retrofit+RxJava實踐小結,此Sample最初是Android MVP示例,後來融合Retrofit和RxJava,供參考。
效果預覽
程式碼演示
public class MainActivity extends MvpActivity<MainPresenter> implements MainView {
@Bind(R.id.text)
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initToolBarAsHome("MVP+Retrofit+Rxjava");
}
@Override
protected MainPresenter createPresenter() {
return new MainPresenter(this);
}
@Override
public void getDataSuccess(MainModel model) {
//介面成功回撥
MainModel.WeatherinfoBean weatherinfo = model.getWeatherinfo();
String showData = getResources().getString(R.string.city) + weatherinfo.getCity()
+ getResources().getString(R.string.wd) + weatherinfo.getWD()
+ getResources().getString(R.string.ws) + weatherinfo.getWS()
+ getResources().getString(R.string.time) + weatherinfo.getTime();
text.setText(showData);
}
@Override
public void getDataFail(String msg) {
toastShow("網路不給力");
}
@Override
public void showLoading() {
showProgressDialog();
}
@Override
public void hideLoading() {
dismissProgressDialog();
}
@OnClick({R.id.button1, R.id.button2})
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
//普通寫法
loadData();
break;
case R.id.button2:
//MVP+Retrofit+Rxjava
mvpPresenter.loadData("101310222");
break;
}
}
private void loadData() {
showProgressDialog();
addSubscription(apiStores.loadData("101190201"),
new ApiCallback<MainModel>() {
@Override
public void onSuccess(MainModel model) {
MainModel.WeatherinfoBean weatherinfo = model.getWeatherinfo();
String showData = getResources().getString(R.string.city) + weatherinfo.getCity()
+ getResources().getString(R.string.wd) + weatherinfo.getWD()
+ getResources().getString(R.string.ws) + weatherinfo.getWS()
+ getResources().getString(R.string.time) + weatherinfo.getTime();
text.setText(showData);
}
@Override
public void onFailure(int code, String msg) {
toastShow(msg);
}
@Override
public void onFinish() {
dismissProgressDialog();
}
});
}
}複製程式碼
詳見部落格
Android MVP+Retrofit+RxJava實踐小結
推薦閱讀
原始碼地址
微信公眾號
歡迎微信掃一掃關注:不止於技術分享,每天進步一點點。