1.定义Contract,用来管理mvp相关接口,实际项目中如果接口过多,也可以将mvp各职接口进行分包管理。
public interface HomeContract{
//View
interface IHomeView{
public void homeView(String message);
}
//presenter
interface IHomePresenter{
public void homePresenter();
}
//model-->presenter
interface IHomeModel{
void homeModel(IModelListener modelListener);
interface IModelListener{
public void onCompletion(String message);
}
}
}
2.定义基类IBaseModel
public class IBaseModel{
}
3.定义子类Model
public class HomeModel extends IBaseModel implements HomeContract.IHomeModel{
@Override
public void homeModel(IModelListener modelListener){
String data = getCurrentData(BASE_URL);
//获取数据
if(modelListener != null){
modelListener.onCompletion(data);
}
}
}
4.定义基类Presenter接口
public interface IBasePresenter<V>{
void registerView(V view);
void unRegisterView();
boolean isViewAttached();
V getView();
}
5.定义BasePresenter
public class BasePresenter<V> implements IBasePresenter<V>{
private WeakReference<V> mWeakReference;
@Override
public void registerView(V view){
mWeakReference = new WeakReference<>(view);
}
@Override
public void unRegisterView(){
if(mWeakReference != null){
mWeakReference.clear();
mWeakReference = null;
}
}
@Override
public boolean isViewAttached(){
return mWeakReference != null && mWeakReference.get() != null;
}
@Override
public V getView(){
return mWeakReference != null : mWeakReference.get() : null;
}
}
6.实际Presenter处理类
public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.IHomePresenter,HomeContract.IModelListener{
private HomeContract.IHomeModel homeModel;
public HomePresenter(HomeContract.IHomeModel homeModel){
this.homeModel = homeModel;
}
@Override
public void homePresenter(){
homeModel.homeModel(this);
}
@Override
public void onCompletion(String message){
if(isViewAttached()){
getView().homeView(message);
}
}
}
7.定义IBaseView
public interface IBaseView{
public Context getViewContext();
public void bindView();
public void unBindView();
}
8.定义BaseActivity中实现Presenter相关方法
public abstract class BaseActivity<P extends BasePresenter> extends Activity implements IBaseView{
protected P mPresenter;
protected Context mContext;
@Override
public void onCreate(Bundle saveData){
mContext = this;
bindView();
}
@Override
public void getViewContext(){
return mContext;
}
@Override
public void bindView(){
mPresenter = createPresenter();
if(mPresenter != null){
mPresenter.registerView(this);
}
}
public abstract P createPresenter();
@Override
public void unBindView(){
if(mPresenter != null){
mPresenter.unRegisterView(this);
}
}
@Override
public void onDestroy(){
unBindView();
}
}
9. 定义具体View(Activity)
public class HomeActivity extends BaseActivity<HomePresenter> implements HomeContract.IHomeView{
@Override
protected void initView() {
setContentView(R.layout.activity_mvp);
}
@Override
public HomePresenter createPresenter(){
return new HomePresenter();
}
@Override
protected void initData() {
if (mPresenter != null){
mPresenter.mainPresenter();
}
}
@Override
public void homeView(String message){
Log.d("mvp_log","[message]---"+message);
}
}