频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
android MVP + dagger2 + Retrofit + Rxjava+okhttp android基础项目框架搭建(2)--之MVP引入
2017-08-10 11:07:38      个评论    来源:lg878398509的专栏  
收藏   我要投稿
android MVP + dagger2 + Retrofit + Rxjava+okhttp    android基础项目框架搭建(2)--之MVP引入,上一篇文章中,我们已经成功的引入了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 {


    @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 getComponent();
}

MainPresenter .java

public class MainPresenter extends RxMvpPresenter {


    @Inject
    public MainPresenter() {


    }


    @Inject
    Application context;


    public void login(String username, String password) {
        getMvpView().loginSuccess("MVP成功搭建");
    }
}

RxMvpPresenter.java

public class RxMvpPresenter extends BaseMvpPresenter {


    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 implements MvpPresenter {


    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 {



    @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**
点击复制链接 与好友分享!回本站首页
上一篇:android MVP + dagger2 + Retrofit + Rxjava+okhttp android基础项目框架搭建(3)--完美实现
下一篇:自定义View
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站