開發一個App,和起房子應該有殊途同歸之處,起房子須要畫好設計圖紙,而咱們開發App則須要先設計好App整個架構模式。目前Android通常有MVC、MVP和MVVM,本文則先來講說MVP架構。在瞭解MVP架構以前,有人可能會說,MVP架構是否是有點落後了,可是我想說,若是你公司有老項目,他就是用MVP架構寫的,這時候咱們MVP知識是否是就派上用場了;任何架構都有它存在的理由,學習架構的思想纔是關鍵。MVP分別表明Model、View、Presenter三個英文字母,和傳統的MVC 相比,C替換成了P。Presenter英文單詞有主持人意思,也就是說Presenter是View 和 Model 的主持人,按照慣例咱們先來看兩張圖。android
/**
* @author maoqitian
* @Description View 的基類
*/
public interface BaseView {
/**
* 正常顯示
*/
void showNormal();
/**
* 顯示錯誤
*/
void showError();
/**
* 正在加載
*/
void showLoading();
/**
* 顯示錯誤信息
* @param errorMsg 錯誤信息
*/
void showErrorMsg(String errorMsg);
}
複製代碼
/**
* @author maoqitian
* @Description Presenter 基類接口
*/
public interface AbstractBasePresenter<T extends BaseView> {
/**
* 綁定View
* @param view
*/
void attachView(T view);
/**
* 解綁View
*/
void detachView();
}
複製代碼
/**
* @author maoqitian
* @Description activity基類
*/
public abstract class AbstractActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
onViewCreated();
initToolbar();
initEventAndData();
}
/**
* view 的建立 留給子類實現
*/
protected abstract void onViewCreated();
/**
* 初始化 toolbar
*/
protected abstract void initToolbar();
/**
* 初始化數據留給子類實現
*/
protected abstract void initEventAndData();
/**
* 獲取佈局對象 留給子類實現
*/
protected abstract int getLayout();
}
複製代碼
/**
* @author maoqitian
* @Description MVP BaseActivity 基類
*/
public abstract class BaseActivity <T extends AbstractBasePresenter> extends AbstractActivity implements BaseView{
protected T mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mPresenter = createPresenter();
super.onCreate(savedInstanceState);
}
@Override
protected void onViewCreated() {
if (mPresenter != null) {
mPresenter.attachView(this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mPresenter != null){
mPresenter.detachView();
mPresenter = null;
}
}
/**
* 建立Presenter
*/
protected abstract T createPresenter();
@Override
public void showNormal() {
}
@Override
public void showError() {
}
@Override
public void showLoading() {
}
@Override
public void showErrorMsg(String errorMsg) {
}
}
複製代碼
/**
* @author maoqitian
* @Description 基於Presenter封裝
*/
public class RxBasePresenter<T extends BaseView> implements AbstractBasePresenter<T>{
protected T mView;
private DataClient mDataClient;
public RxBasePresenter(DataClient dataClient){
this.mDataClient=dataClient;
}
@Override
public void attachView(T view) {
this.mView=view;
}
@Override
public void detachView() {
this.mView = null;
}
}
複製代碼
public interface MainContract {
interface MainView extends BaseView{
void showMainData();
}
interface MainActivityPresenter extends AbstractBasePresenter<MainView>{
void getMainData();
}
}
複製代碼
/**
* @author maoqitian
* @Description MainPresenter (Presenter)
*/
public class MainPresenter extends RxBasePresenter<MainContract.MainView> implements MainContract.MainActivityPresenter {
//(Model)
private DataClient mDataClient;
public MainPresenter(DataClient dataClient) {
super(dataClient);
this.mDataClient = dataClient;
}
@Override
public void attachView(MainContract.MainView view) {
super.attachView(view);
}
//獲取數據
@Override
public void getMainData() {
//mDataClient 網絡請求獲取數據
.......
// 數據獲取成功展現數據
mView.showMainData();
}
}
/**
* @author maoqitian
* @Description MainActivity (View)
*/
public class MainActivity extends BaseActivity<MainPresenter>implements MainContract.MainView{
..........
@Override
protected MainPresenter createPresenter() {
return new MainPresenter(new DataClient());
}
@Override
protected void initEventAndData() {
mPresenter.getMainData();
}
@Override
public void showMainData() {
//顯示數據
}
}
複製代碼
dependencies {
implementation 'com.google.dagger:dagger:2.22.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.22.1'
//
implementation 'com.google.dagger:dagger-android:2.22.1'
implementation 'com.google.dagger:dagger-android-support:2.22.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.22.1'
}
複製代碼
public class MainPresenter extends RxBasePresenter<MainContract.MainView> implements MainContract.MainActivityPresenter {
private DataClient mDataClient;
//@Inject註解表示Dagger2 能夠從這獲取Presenter 實例
@Inject
public MainPresenter(DataClient dataClient) {
super(dataClient);
this.mDataClient = dataClient;
}
}
複製代碼
/**
* @author maoqitian
* @Description MVP BaseActivity 基類
*/
public abstract class BaseActivity <T extends AbstractBasePresenter> extends AbstractSimpleActivity implements BaseView, HasFragmentInjector,HasSupportFragmentInjector {
//Presenter 對象注入 (注意不能使用 private )
@Inject
protected T mPresenter;
//手動實現DaggerAppCompatActivity功能
@Inject DispatchingAndroidInjector<Fragment> supportFragmentInjector;
@Inject DispatchingAndroidInjector<android.app.Fragment> frameworkFragmentInjector;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//必須在super.onCreate以前調用AndroidInjection.inject
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
}
@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return supportFragmentInjector;
}
@Override
public AndroidInjector<android.app.Fragment> fragmentInjector() {
return frameworkFragmentInjector;
}
}
複製代碼
/**
* @author maoqitian
* @Description MainActivity 能夠提供的注入對象Module
* @Time 2019/3/27 0027 23:59
*/
@Module
public abstract class MainActivityModule {
@ActivityScope
@Binds
abstract MainContract.MainActivityPresenter bindPresenter(MainPresenter presenter);
}
複製代碼
/**
* @author maoqitian
* @Description 用於生成Activity注入器的Module,使用@ContributesAndroidInjector註解並指定modules爲
* @Time 2019/4/14 0014 14:09
*/
@Module
public abstract class ActivityBindingModule {
@ActivityScope
@ContributesAndroidInjector(modules = MainActivityModule.class)
abstract MainActivity contributeMainActivity();
}
複製代碼
@Module
public class MyAppModule {
@Provides
@Singleton
public DataClient providerDataClient(){
return new DataClient();
}
}
複製代碼
@Singleton
@Component(modules = {
MyAppModule.class,
ActivityBindingModule.class,
AndroidSupportInjectionModule.class
})
public interface AppComponent extends AndroidInjector<MyApplication> {
}
複製代碼
public class MyApplication extends DaggerApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.create();
}
}
複製代碼