最近喜欢上了mvp,所以也学习了网站上别人的mvp自己写了一个简易的mvp,我不太确定我这是不是标准的mvp,但我感觉分离的挺开的。
这里的mvp我是结合了retrofit+rxjava,全是简易的,因为都是刚开始学习,希望大家多多指点。
其实在我看来mvp就是将v层和m层完全分离开来,通过p层去控制v层和m层(也就是在p层中调用v层或m层的方法)下面我直接将代码贴出来吧,很容易理解的。
v层:
public class MainActivity extends AppCompatActivity implements MainActivityInterface {
@Bind(R.id.click_me_BN)
Button clickMeBN;
@Bind(R.id.result_tv)
TextView resultTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.click_me_BN)
public void onClick() {
MainPresent mainPresent = new MainPresent(this);
mainPresent.loadData();
}
@Override
public void showData(MovieEntity movieEntity) {
resultTv.setText(movieEntity.getTitle());
}
@Override
public void showError() {
}
}
public interface MainActivityInterface {
void showData(MovieEntity movieEntity);
}
p层:
public class MainPresent implements MainPresentInterface,MainModel.OngetResult {
private final MainModel mMainModel;
public MainActivityInterface mActivityInterface;
public MainPresent(MainActivityInterface activityInterface) {
getInterfaceView(activityInterface);
mMainModel = new MainModel(this);
}
@Override
public void addViewData(MovieEntity movieEntity) {
mActivityInterface.showData(movieEntity);
}
@Override
public void getInterfaceView(MainActivityInterface mainActivityInterface) {
this.mActivityInterface = mainActivityInterface;
}
@Override
public void loadData() {
mMainModel.loadData();
}
@Override
public void getData(MovieEntity movieEntity) {
//成功获取数据,取消进度条
addViewData(movieEntity);
}
@Override
public void error(Throwable e) {
//失败获取数据,取消进度条并进行其他操作
}
}
public interface MainPresentInterface {
void addViewData(MovieEntity movieEntity);
void getInterfaceView(MainActivityInterface mainActivityInterface);
void loadData();
}
M层:
public class MainModel implements MainModelInterface {
private MainPresent mMainPresent;
private OngetResult mOngetResult;
public interface OngetResult {
void getData(MovieEntity movieEntity);
void error(Throwable e);
}
public MainModel(MainPresent mainPresent) {
this.mMainPresent = mainPresent;
}
/**
* 进行网络请求
* https://api.douban.com/v2/movie/top250?start=0&count=10
*/
@Override
public void loadData() {
String baseUrl = "https://api.douban.com/v2/movie/";
ApiManager.getWriData(baseUrl,0,10)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MovieEntity>() {
@Override
public void onCompleted() {
//成功
}
@Override
public void onError(Throwable e) {
//出错
mOngetResult.error(e);
}
@Override
public void onNext(MovieEntity movieEntity) {
//拿数据
mOngetResult.getData(movieEntity);
}
}
);
}
}
public interface MainModelInterface {
void loadData();
}
APImanager:
public class ApiManager {
private static String baseUrl;
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
private static MovieService movieService = retrofit.create(MovieService.class);
public static Observable<MovieEntity> getWriData(String url, int start, int count) {
baseUrl = url;
return movieService.getTopMovie(start, count);
}
}
MovieService :
public interface MovieService {
@GET("top250")
Observable<MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count);
}