介绍http://www.jianshu.com/p/39d1df6c877d
大致实现步骤如下
PresenterComponent类
代码语言:javascript复制@Singleton
@Component(modules = {PresenterModule.class})
public interface PresenterComponent {
/**
* 注入点
* @param activity 表示需要使用DaggerPresenterComponent.create().inject(this);注入的地方,
* 注意,此处一定不要使用Activity,需要使用MainActivity,否则的话会报空指针异常。
* 因为这里的注入点是什么,就会到该类里面去找。如果写Activity,就会到Activity里面去找,
* 而Activity中并没有@inject,即没有需要注入的地方,所以在生成的DaggerPresenterComponent
* 中,方法就不会被调用。
*/
void inject( MainActivity activity);
}
PresenterModule类
代码语言:javascript复制 DownloadQuanCryPre downloadQuanCryPre;
DownloadQuanDataPre downloadQuanDataPre;
public PresenterModule(MainActivity activity) {
downloadQuanCryPre =new DownloadQuanCryPre(activity,activity);
downloadQuanDataPre =new DownloadQuanDataPre(activity,activity);
}
}
在Activity
代码语言:javascript复制 @Inject
public DownloadQuanCryPre downloadQuanCryPre;
@Inject
public DownloadQuanDataPre downloadQuanDataPre;
//oncreate()方法中
DaggerPresenterComponent.builder().presenterModule(new PresenterModule(this)).build().inject(this);
BasePresenter(注:BasePresenter<IRequestCollectView,BaseActivity> 可以引入BaseActivity,这样可以在多个Activity中复用)
代码语言:javascript复制public class BasePresenter<V, T> implements LifeCycleListener {
protected Reference<V> mViewRef;
protected V mView;
protected Reference<T> mActivityRef;
protected T mActivity;
public BasePresenter(V view, T activity) {
attachView(view);
attachActivity(activity);
setListener(activity);
}
/**
* 设置生命周期监听
*
* @author ZhongDaFeng
*/
private void setListener(T activity) {
if (getActivity() != null) {
if (activity instanceof BaseActivity) {
((BaseActivity) getActivity()).setOnLifeCycleListener(this);
} else if (activity instanceof BaseFragmentActivity) {
((BaseFragmentActivity) getActivity()).setOnLifeCycleListener(this);
}
}
}
/**
* 关联
*
* @param view
*/
private void attachView(V view) {
mViewRef = new WeakReference<V>(view);
mView = mViewRef.get();
}
/**
* 关联
*
* @param activity
*/
private void attachActivity(T activity) {
mActivityRef = new WeakReference<T>(activity);
mActivity = mActivityRef.get();
}
/**
* 销毁
*/
private void detachView() {
if (isViewAttached()) {
mViewRef.clear();
mViewRef = null;
}
}
/**
* 销毁
*/
private void detachActivity() {
if (isActivityAttached()) {
mActivityRef.clear();
mActivityRef = null;
}
}
/**
* 获取
*
* @return
*/
public V getView() {
if (mViewRef == null) {
return null;
}
return mViewRef.get();
}
/**
* 获取
*
* @return
*/
public T getActivity() {
if (mActivityRef == null) {
return null;
}
return mActivityRef.get();
}
/**
* 是否已经关联
*
* @return
*/
public boolean isViewAttached() {
return mViewRef != null && mViewRef.get() != null;
}
/**
* 是否已经关联
*
* @return
*/
public boolean isActivityAttached() {
return mActivityRef != null && mActivityRef.get() != null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
}
@Override
public void onStart() {
}
@Override
public void onRestart() {
}
@Override
public void onResume() {
}
@Override
public void onPause() {
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
detachView();
detachActivity();
}
}
代码语言:javascript复制//presenter 继承上面的BasePresenter
if (getView() != null&&getActivity()!=null){
if(!quanCryBean.isError())
getView().loadCry(quanCryBean.getQuan_category());
else
getView().loadCryFail(quanCryBean.getError_msg());
}