星空网 > 软件开发 > 操作系统

MVP解析

一套可以直接复制使用的MVP框架

通过对MVP设计模式学习,对MVP也有了一个初步的认识,以登录Login模块为例,封装MVP如下:

package com.example.administrator.frameapp.api;/** * 存放url的接口 * Created by Zyh on 2016/11/17. */public interface ApiUrl {  String IP="http://192.168.8.4/tp3/";  String BASEURL=IP+"api.php/Home/";}

package com.example.administrator.frameapp.api;import io.reactivex.Flowable;import retrofit2.http.Field;import retrofit2.http.FormUrlEncoded;import retrofit2.http.POST;/** * Created by Zyh on 2016/11/17. */public interface ApiService {  @FormUrlEncoded  @POST("login/login")  Flowable<ApiResult> login(@Field("name") String name, @Field("password") String password);}

package com.example.administrator.frameapp.api;/** * Created by Zyh on 2016/11/17. */public class ApiResult<T> {  private int code;  private String Msg;  private T data;  public int getCode() {    return code;  }  @Override  public String toString() {    return "ApiResult{" +        "code=" + code +        ", Msg='" + Msg + '\'' +        ", data=" + data +        '}';  }  public void setCode(int code) {    this.code = code;  }  public String getMsg() {    return Msg;  }  public void setMsg(String msg) {    Msg = msg;  }  public T getData() {    return data;  }  public void setData(T data) {    this.data = data;  }}

package com.example.administrator.frameapp.api;import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;import okhttp3.OkHttpClient;import okhttp3.logging.HttpLoggingInterceptor;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;/** * Created by Zyh on 2016/11/17. */public class Api {  private Retrofit mRetrofit;  public ApiService mApiservice;  private Api() {    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//请求的内容和响应的内容都存在这个系统的BODY中    OkHttpClient mOkHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();    mRetrofit = new Retrofit.Builder()        .client(mOkHttpClient)        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())        .addConverterFactory(GsonConverterFactory.create())        .baseUrl(ApiUrl.BASEURL)        .build();    mApiservice = mRetrofit.create(ApiService.class);  }  //静态内部类的单例模式:内部类决定了什么时候加载他就什么时候进行加载,  private static class SingleHolder {    private static final Api INSTANCE = new Api();  }  public static Api getInstance() {    return SingleHolder.INSTANCE;  }}

 

package com.example.administrator.frameapp.ui.base;/** * 创建base类是为了统一管理 * BasePresent是抽象类 * 将model和view关联起来 * Created by Zyh on 2016/11/17. */public abstract class BasePresent<M,V> {  public M mModel;  public V mView;  public void setVM(V v,M m){    //这个方法将LoginPresenter中方法中类型映射成具体的类型    this.mModel=m;    this.mView=v;  }}

package com.example.administrator.frameapp.ui.base;/** * Created by Zyh on 2016/11/17. */public interface BaseView {}

package com.example.administrator.frameapp.ui.base;/** * Created by Zyh on 2016/11/17. */public interface BaseModel {}

 




原标题:MVP解析

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

防不胜防!这些行为居然也算侵权?别再踩雷了:https://www.ikjzd.com/articles/1564793852882391041
封店警告!跨境卖家注意,圣诞旺季这些爆款千万别碰……:https://www.ikjzd.com/articles/1564794758371811330
免费下载!成为高阶大卖的必备资料(限时删):https://www.ikjzd.com/articles/1564798829682040834
惊闻亚马逊美国仓库起火,各路卖家喜忧参半,且看咋回事?:https://www.ikjzd.com/articles/15648
在国内能开TikTok小店卖货吗?卖什么商品比较好?:https://www.ikjzd.com/articles/1564810271762223106
已故律师竟还在递交商标申请!?2200+商标将被作废!:https://www.ikjzd.com/articles/1564819480815935489
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流