一、添加依赖
compile'com.squareup.okhttp3:okhttp:3.8.1'
compile'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile'io.reactivex:rxjava:1.1.6'
// Retrofit的rx解析库
compile'com.squareup.retrofit2:retrofit:2.1.0'
compile'com.squareup.retrofit2:adapter-rxjava:2.1.0'
// Retrofit的gson库字符串库
compile'com.squareup.retrofit2:converter-gson:2.1.0'
compile'com.squareup.retrofit2:converter-scalars:2.1.0'
compile'com.orhanobut:logger:1.8'
//解决Android3.0以后不能butterknife问题
implementation'com.jakewharton:butterknife:8.4.0'
annotationProcessor'com.jakewharton:butterknife-compiler:8.4.0'
compile'io.reactivex:rxandroid:1.2.1'
二、Tutils tmvp工具类
public class TUtil {
public static T getT(Object o,int i) {
try {
return ((Class) ((ParameterizedType) (o.getClass()
.getGenericSuperclass())).getActualTypeArguments()[i])
.newInstance();
}catch (InstantiationException e) {
e.printStackTrace();
}catch (IllegalAccessException e) {
e.printStackTrace();
}catch (ClassCastException e) {
e.printStackTrace();
}
return null;
}
public static Class forName(String className) {
try {
return Class.forName(className);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
三、RetrofitManager 创建retrofit对象
public class RetrofitManager {
//地址
private StringBaseURL = Contents.BASEURL;
private static OkHttpClientmOkHttpClient;
private RetrofitmRetrofit;
/**
* 构建Retrofit
*
* @return
*/
public Retrofit build() {
initOkHttpClient();
initRetrofit();
return mRetrofit;
}
public RetrofitManager() {
}
protected void initRetrofit() {
mRetrofit =new Retrofit.Builder()
.baseUrl(BaseURL)
.client(mOkHttpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
}
private void initOkHttpClient() {
//拦截器,用于输出网络请求和结果的 Log
HttpLoggingInterceptor interceptor =new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//初始化OkHttpClient
mOkHttpClient =new OkHttpClient.Builder()
.addInterceptor(interceptor)
.retryOnConnectionFailure(true)
.connectTimeout(15, TimeUnit.SECONDS)
.build();
}
}
四、Contents 网址 App
public class Contents {
public static final StringBASEURL="http://123.57.133.62:8180/lifeshop/";
}
public class Appextends Application {
public static AppCompatActivitymActivity;
public static FragmentmLastFragment;
private static Contextcontext;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
public static Context getContext() {
return context;
}
}
五、LoginContract(契约类),有了契约类后自动生成相关的base类
public class LoginContract {
public interface Viewextends BaseView {
void loginSucc(String str);
void loginFail(String result);
}
public interface Modelextends BaseModel {
rx.Observable loginByPsw(String phone, String code);
}
public abstract static class Presenterextends BasePresenter {
abstract void phoneLogin(String phone,String psw);
}
}
六、BaseModel初始化retrofit对象
public interface BaseModel {
public RetrofitManagermRetrofitManager =new RetrofitManager();
}
七、BasePresenter 初始化presenter对象
public class BasePresenter {
public M mModel;
public V mView;
public void setMV(M m,V v){
this.mModel=m;
this.mView=v;
}
}
八、BaseActivity
public abstract class BaseActivityextends Activityimplements BaseView {
public T mPresenter;
public E mModel;
private FragmentManagermanager;
private FragmentTransactiontransaction;
private Fragmentfragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
protected void init() {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(getLayoutResID());
mPresenter = TUtil.getT(this,0);
mModel = TUtil.getT(this,1);
if (this instanceof BaseView)mPresenter.setMV(mModel,this );
initFragmentManager();
initView();
}
private void initFragmentManager() {
manager = App.mActivity.getSupportFragmentManager();
transaction =manager.beginTransaction();
}
public Fragment add(int containerId, Class fragmentClass,boolean isChild, Map map){
//用Fragment的名字设置一个tag值
String tag = fragmentClass.getSimpleName();
//使用tag值来判断当前Fragment是否实例化
fragment =manager.findFragmentByTag(tag);
//
if (fragment ==null){
try {
//使用单例创建Fragment对象
fragment = fragmentClass.newInstance();
//使用add方法添加到容器中
transaction.add(containerId,fragment,tag);
//调用添加回退栈方法进行栈管理
//addToBackStack(): 添加回退栈
transaction.addToBackStack(tag);
}catch (InstantiationException e) {
e.printStackTrace();
}catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (!isChild ) {
if (App.mLastFragment !=null){
transaction.hide(App.mLastFragment);
}
}
if (map!=null) {
Bundle bundle=new Bundle();
for (Map.Entry entry : map.entrySet()) {
bundle.putString(entry.getKey(),entry.getValue());
}
}
transaction.show(fragment);
return fragment;
}
/**
* 获得Layout文件id
*
* @return
*/
protected abstract int getLayoutResID();
protected abstract void initView();
}
九、BaseFragment
public abstract class BaseFragmentextends Fragment {
protected ViewrootView;
public T mPresenter;
public E mModel;
private FragmentManagermanager;
private FragmentTransactiontransaction;
private Fragmentfragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView ==null)
rootView = inflater.inflate(getLayoutResource(), container,false);
mPresenter = TUtil.getT(this,0);
mModel= TUtil.getT(this,1);
if (this instanceof BaseView) {
mPresenter.setMV(mModel,this);
}
initFragmentManager();
initView();
return rootView;
}
private void initFragmentManager() {
manager = App.mActivity.getSupportFragmentManager();
transaction =manager.beginTransaction();
}
//获取布局文件
protected abstract int getLayoutResource();
//简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
public abstract void initPresenter();
//初始化view
protected abstract void initView();
}
希望对您有帮助,关注一下呗!!