上一篇文章中,我们已经成功的引入了Dagger2;今天我们将继续使用android MVP模式对代码进行实现。
1 将MainActivity.java文件移动到ui包下,并在ui包下建立view包; view包下存放接口。包及类结构如图
2 新建presenter包,并在presenter下建立相关的类, 如图
- 说明:图中RxMapPresenter为Presenter的基类, 该类继承BaseMapPresenter类; BaseMapPresenter提供子类实例化Presenter与View之间的接口对象的方法。RxMapPresenter 负责实例化CompositeSubscription对象, 子类使用该对象进行网络请求。
MainActivity.java代码如下
public class MainActivity extends BaseAcitvity implements MainView, HasComponent<MainComponent> {
@BindView(R.id.test)
TextView test;
@Inject
MainPresenter mainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
getComponent().inject(this);
//重要
mainPresenter.attachView(this);
}
@OnClick(R.id.test)
public void onClick() {
mainPresenter.login("", "");
}
@Override
public void loginSuccess(String userInfo) {
Toast.makeText(this, userInfo, Toast.LENGTH_LONG).show();
}
@Override
public void loginFail() {
}
@Override
public void loginException() {
}
@Override
public MainComponent getComponent() {
return DaggerMainComponent.builder()
.applicationComponent(Application.get(this).getComponent())
.activityModule(new ActivityModule(this))
.mainModule(new MainModule())
.build();
}
@Override
protected void onDestroy() {
super.onDestroy();
mainPresenter.detachView();
}
}
BaseAcitvity.java
public class BaseAcitvity extends Activity {
private ActivityComponent mActivityComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public ActivityComponent getActivityComponent() {
if (mActivityComponent == null) {
mActivityComponent = DaggerActivityComponent.builder()
.activityModule(new ActivityModule(this))
.applicationComponent(Application.get(this).getComponent())
.build();
}
return mActivityComponent;
}
}
MainView.java
public interface MainView extends MvpView{
void loginSuccess(String userInfo);
void loginFail();
void loginException();
}
HasComponent.java
public interface HasComponent<C> {
C getComponent();
}
MainPresenter .java
public class MainPresenter extends RxMvpPresenter<MainView> {
@Inject
public MainPresenter() {
}
@Inject
Application context;
public void login(String username, String password) {
getMvpView().loginSuccess("MVP成功搭建");
}
}
RxMvpPresenter.java
public class RxMvpPresenter<V extends MvpView> extends BaseMvpPresenter<V> {
protected CompositeSubscription mCompositeSubscription;
@Override
public void attachView(V mvpView) {
super.attachView(mvpView);
mCompositeSubscription = new CompositeSubscription();
}
@Override
public void detachView() {
super.detachView();
mCompositeSubscription.clear();
mCompositeSubscription = null;
}
}
BaseMvpPresenter.java
public class BaseMvpPresenter<V> implements MvpPresenter<V> {
private V mMvpView;
@Override
public void attachView(V mvpView) {
mMvpView = mvpView;
}
@Override
public void detachView() {
mMvpView = null;
}
public boolean isViewAttached() {
return mMvpView != null;
}
public V getMvpView() {
return mMvpView;
}
public void checkViewAttached() {
if (!isViewAttached()) throw new MvpViewNotAttachedException();
}
public static class MvpViewNotAttachedException extends RuntimeException {
public MvpViewNotAttachedException() {
super("Please call Presenter.attachView(MvpView) before" +
" requesting data to the Presenter");
}
}
}
MvpPresenter.java
public interface MvpPresenter<V> {
@UiThread
void attachView(V view);
@UiThread
void detachView();
}
BaseAcitvity.java
public class BaseAcitvity extends Activity {
private ActivityComponent mActivityComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public ActivityComponent getActivityComponent() {
if (mActivityComponent == null) {
mActivityComponent = DaggerActivityComponent.builder()
.activityModule(new ActivityModule(this))
.applicationComponent(Application.get(this).getComponent())
.build();
}
return mActivityComponent;
}
}
MvpView.java
public interface MvpView {
}
3. 运行项目, 点击屏幕上的HelloWold!文字, 如果出现 “MVP搭建成功”的文字说明android MVP环境搭建成功,如图:。
下一篇我们将讲解如何引入Retrofit RxJava