MVP是什么
MVP簡介
MVP(Model-View-Presenter)是MVC模式的改良,由IBM的子公司Taligent提出,
和MVC的相同之處在于:Controller/Presenter負責業務邏輯,Model管理資料,View負責顯示,
1.各部分之間的通信,都是雙向的.
View <- (雙向) -> Presenter <- (雙向) ->Model
2.View 與 Model不發生練習,都通過Presenter傳遞.
3.View非常薄,不部署任何業務邏輯,被稱為"被動視圖"(Prassive).既沒有任何主動性, 而Presenter非常厚,所有邏輯部署都在這里,

一 MVP使用
1.需要的依賴整理
//異步執行緒
api 'io.reactivex.rxjava2:rxjava:2.2.6'
api 'io.reactivex.rxjava2:rxandroid:2.1.1'
//網路框架
api 'com.squareup.okhttp3:okhttp:3.12.1'
//日志攔截器
api 'com.squareup.okhttp3:logging-interceptor:3.11.0'
//網路請求封裝框架
api 'com.squareup.retrofit2:retrofit:2.6.2'
//網路請求物件決議器
api 'com.squareup.retrofit2:converter-gson:2.6.2'
//執行緒例外任務調度
api 'com.squareup.retrofit2:adapter-rxjava2:2.6.2'
//工具類
api 'com.blankj:utilcodex:1.26.0'
2. 一般三步走
mvp層
(1) m層
定義介面
public interface Imodel {
void destroy();
}
參考介面
呼叫銷毀方法
public class BaseModel implements Imodel {
/**
* 銷毀
*/
@Override
public void destroy() {
}
}
(2) v層
定義介面view三個方法
public interface Iview {
void showLoading();
void hideLoading();
void showToast();
}
定義介面IActicity三個方法
public interface IActivity {
int banLoutler();
void initView();
void initData();
}
定義介面Ifragment介面
public interface Ifragment<T> extends IActivity {
<T extends View> T findViewBayId(int id);
}
base
*[因為我一直寫fragment 我這里就先介紹fragment的使用方法,如有需要請聯系我]:
public abstract class BaseFragment extends Fragment implements Ifragment,Iview {
*[這里面就是實作的一些方法 大家自行實作吧,都有提示,abstract 抽象 ,里面有的方法用不到 我們要使用抽象方法進行]:
}
(3) p層
(1) 定義介面
public class Basepresenter<M extends Imodel ,V extends Iview> implements Ipresenter {
protected M mModel;
protected V mView;
public Basepresenter(M mModel, V mView) {
this.mModel = mModel;
this.mView = mView;
}
@Override
public void destroy() {
if (mModel == null){
mModel.destroy();
mModel = null;
}
}
}
(2) 參考介面
public class Basepresenter<M extends Imodel ,V extends Iview> implements Ipresenter {
protected M mModel;
protected V mView;
public Basepresenter(M mModel, V mView) {
this.mModel = mModel;
this.mView = mView;
}
@Override
public void destroy() {
if (mModel == null){
mModel.destroy();
mModel = null;
}
}
}
網路獲取層
(1) okhttp獲取
單例模式
public class RetrofitManager {
private Retrofit retrofit;
public Retrofit getRetrofit() {
if (retrofit == null){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient OKbuilder = new OkHttpClient.Builder()
.writeTimeout(60*1000, TimeUnit.MILLISECONDS)
.readTimeout(60*1000, TimeUnit.MILLISECONDS)
.connectTimeout(60*1000, TimeUnit.MILLISECONDS)
.addInterceptor(httpLoggingInterceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("需要獲取的地址")
.client(OKbuilder)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return retrofit;
}
private static RetrofitManager retrofitManager;
public static RetrofitManager getRetrofitManager() {
if (retrofitManager == null){
retrofitManager = new RetrofitManager();
}
return retrofitManager;
}
}
okgo獲取
(1) 進入
*[皮一下 身為程式員 okgo這么簡單的方法還要來看嗎 嘻嘻嘻]:
CallOberver
定義介面 自己來看吧
public interface Icalloberver<T> {
void suesse(T t);
void fail(String msg);
}
需要進來看
public abstract class CallOberver<T> extends DisposableObserver<T> implements Icalloberver<T> {
@Override
public void fail(String msg) {
if (BuildConfig.DEBUG) Log.d("CallOberver", msg);
}
@Override
public void onNext(T t) {
suesse(t);
}
@Override
public void onError(Throwable e) {
fail(e.getMessage());
}
@Override
public void onComplete() {
}
}
二 使用方法需要記得聯系我 下一期繼續
記得聯系我
私信我
看到這了繼續看一下吧 祝您心情好
鍥而不舍?
祝你開心 心想事成 步步高升
*[相信自己 努力總會成功 加油 程式員這條路不好走 我們大家一起努力]:
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/272012.html
標籤:其他
上一篇:Flutter學習第十五天:2021年最新版超詳細Flutter實作Mob+SMSSDK手機驗證碼登錄實作,Android和Flutter混合開發?
下一篇:Vue專案開發之專案初始化
