> 文档中心 > Android BaseMvp 的使用

Android BaseMvp 的使用

BaseMvp 的简单封装

上一篇介绍了BaseMvp 的封装,这一篇来简单的介绍一下具体的使用方法。

一、BaseActivity 的使用,请忽略类名,这里我后期会写一个简单的dome,就用login 类名做了代替。**

1、先创建一个契约接口,在这里,我们提供M、V、P 层中的接口方法

package com.traveleasy.electricity.contract;import com.traveleasy.electricity.Base.IBasePresenter;import com.traveleasy.electricity.Base.IBaseView;import okhttp3.Callback;/** * 契约接口,可以直观的看到M,V,P 层接口中提供的方法 */public interface LoginContract {    interface LoginMadel{ void requestBaidu(Callback callback);    }    interface LoginView extends IBaseView{ void showDialog(); void succes(String content);    }    interface LoginPresenter extends IBasePresenter{ void handlerData();    }}

2、新建model 层,实现数据的请求获取

package com.traveleasy.electricity.model;import com.traveleasy.electricity.Base.BaseModel;import com.traveleasy.electricity.contract.LoginContract;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;/** * model 层,请求网络或数据库,提供数据源(原始数据) */public class LoginModel extends BaseModel implements LoginContract.LoginMadel {    @Override    public void requestBaidu(Callback callback) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder()  .url("https://www.baidu.com/")  .build(); client.newCall(request).enqueue(callback);    }}

3、实现Presenter 层,承担业务逻辑处理,数据的处理等

package com.traveleasy.electricity.presenter;import android.util.Log;import com.traveleasy.electricity.Base.BasePresenter;import com.traveleasy.electricity.Base.IBaseView;import com.traveleasy.electricity.contract.LoginContract;import com.traveleasy.electricity.model.LoginModel;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;/** * presenter 层,承担业务逻辑处理,数据的处理等 */public class LoginPresenter extends BasePresenter<LoginContract.LoginView, LoginModel> implements LoginContract.LoginPresenter {    @Override    public void handlerData() { getView().showDialog(); /**  * 发起请求,获取回调函数  */ getModel().requestBaidu(new Callback() {     @Override     public void onFailure(Call call, IOException e) {     }     @Override     public void onResponse(Call call, Response response) throws IOException {  String content = response.body().string();  if (getView() != null) {      getView().succes(content);  }     } });    }    @Override    public void detechView() { super.detechView(); /**  * 释放内存,关闭网络请求,关闭线程等操作  */ Log.d("=======", "detechView: 解除绑定,释放内存");    }}

4、View 视图层,这里我们处理页面的UI 部分

package com.traveleasy.electricity;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import android.app.ProgressDialog;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.traveleasy.electricity.Base.BaseActivity;import com.traveleasy.electricity.Base.BasePresenter;import com.traveleasy.electricity.contract.LoginContract;import com.traveleasy.electricity.inject.InjectPresenter;import com.traveleasy.electricity.presenter.LoginPresenter;public class MainActivity extends BaseActivity implements LoginContract.LoginView {    private TextView tv;//    依赖注入,解决多个 Presenter 的问题    @InjectPresenter    private LoginPresenter mPresenter;    @Override    protected void initLayout(@Nullable Bundle savedInstanceState) { setContentView(R.layout.activity_main);    }    public void checkName(View view){ Intent intent = new Intent(this, SecondActivity.class); startActivity(intent);    }    @Override    protected void initViews() { tv = $(R.id.tv);    }    @Override    protected void initData() { mPresenter.handlerData();    }    @Override    public void showDialog() { ProgressDialog dialog = new ProgressDialog(getContext()); dialog.show(); new Handler().postDelayed(new Runnable() {     @Override     public void run() {  dialog.dismiss();     } }, 1500);    }    @Override    public void succes(String content) { runOnUiThread(new Runnable() {     @Override     public void run() {  Toast.makeText(MainActivity.this, "" + content, Toast.LENGTH_SHORT).show();  tv.setText(content);     } });    }}

二、BaseFragment 的使用其实和activity的差不多,就不一一介绍了,直接看代码

package com.traveleasy.electricity.contract;import com.traveleasy.electricity.Base.IBasePresenter;import com.traveleasy.electricity.Base.IBaseView;import okhttp3.Callback;public interface SecondContract {     interface SecondModel {  void requestBaidu(Callback callback);    }     interface SecondView extends IBaseView {  void showDialog();  void succes(String context);    }    interface SecondPresenter extends IBasePresenter {  void handleData();    }}
package com.traveleasy.electricity.model;import com.traveleasy.electricity.Base.BaseModel;import com.traveleasy.electricity.contract.SecondContract;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;public class SeconModel extends BaseModel implements SecondContract.SecondModel {    @Override    public void requestBaidu(Callback callback) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder()  .url("https://www.baidu.com/")  .build(); client.newCall(request).enqueue(callback);    }}
package com.traveleasy.electricity.presenter;import com.traveleasy.electricity.Base.BasePresenter;import com.traveleasy.electricity.contract.SecondContract;import com.traveleasy.electricity.model.SeconModel;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;public class SecondPresenter extends BasePresenter<SecondContract.SecondView, SeconModel> implements SecondContract.SecondPresenter {    @Override    public void handleData() { getView().showDialog(); getModel().requestBaidu(new Callback() {     @Override     public void onFailure(Call call, IOException e) {     }     @Override     public void onResponse(Call call, Response response) throws IOException {  String context = response.body().string();  getView().succes(context);     } });    }}

这里新建了一个activity,没有继承base,就是一个fragment 的调用

package com.traveleasy.electricity;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class SecondActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); /**  * 开启一个fragment  */ getSupportFragmentManager().beginTransaction().replace(R.id.second_container, new SecondFragment()).commit();    }}
package com.traveleasy.electricity;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;import androidx.annotation.Nullable;import com.traveleasy.electricity.Base.BaseFragment;import com.traveleasy.electricity.contract.SecondContract;import com.traveleasy.electricity.inject.InjectPresenter;import com.traveleasy.electricity.presenter.SecondPresenter;public class SecondFragment extends BaseFragment implements SecondContract.SecondView {    private TextView tvFragment;    @InjectPresenter    private SecondPresenter mPresenter;    @Override    protected int setLayout() { return R.layout.fragment_second;    }    @Override    protected void initViews(@Nullable Bundle savedInstanceState) { tvFragment = $(R.id.tv_fragment);    }    @Override    protected void initData() { mPresenter.handleData();    }    @Override    public void showDialog() {    }    @SuppressWarnings("ConstantConditions")    @Override    public void succes(String context) { getActivity().runOnUiThread(new Runnable() {     @Override     public void run() {  Toast.makeText(getContext(), "" + context, Toast.LENGTH_SHORT).show();  tvFragment.setText(context);     } });    }}

源码下载