Android中利用泛型簡化MVP

oden發表於2016-10-23

簡介

封裝MvpFragment以及MvpPresenter,簡化MVP的構建,達到偷懶的目的。
可以參考之前的另一篇文章:
Android Mvp實踐

最終使用效果

Fragment和Presenter只需分別繼承MvpFragmen、MvpPresenter即可進行繫結。

Activity

Activity容器,裡面放置了兩個Fragment,在activity中將Fragment和其present繫結。

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    FragmentManager fm;
    private Fragment mFragmentNow;
    MapFragment mapFragment  = new MapFragment_();
    UserFragment userFragment = new UserFragment_();

    @Bean
    MapPresenter mapPresenter;

    @Bean
    UserPresenter userPresenter;

    @AfterViews
    void init() {
        setDefaultFragment();
        mapPresenter.setView(this, mapFragment); //view和presenter繫結
        userPresenter.setView(this, userFragment);//view和presenter繫結
    }

    private void setDefaultFragment() {
        fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.fragcontent, mapFragment);
        transaction.commit();
        mFragmentNow = mapFragment;
    }

    private void switchContent(Fragment from, Fragment to) {
        if (mFragmentNow != to) {
            mFragmentNow = to;
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) {    // 先判斷是否被add過
                transaction.hide(from).add(R.id.fragcontent, to).commit(); // 隱藏當前的fragment,add下一個到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隱藏當前的fragment,顯示下一個
            }
        }
    }
//點選切換fragment略..
}複製程式碼

Contract

Presenter和View的介面

public class MapContract {

    public interface View extends BaseView<Presenter> {
     //...
    }

    public interface Presenter extends BasePresenter {
     //...
    }

}複製程式碼

View層

@EFragment(R.layout.fragment_map)
public class MapFragment extends MvpFragment<MapContract.Presenter> implements MapContract.View{
   //...實現介面中的方法
}複製程式碼

Presenter層

@EBean
public class MapPresenter extends MvpPresenter<MapContract.View> implements MapContract.Presenter {
   //...實現介面中的方法
}複製程式碼

這樣,簡單地就將Presenter層和View關聯起來,在各自的層中處理不同的業務。

實現過程

base類

public interface BaseView<T> {
    void setPresenter(T presenter);
}複製程式碼
public interface BasePresenter {

}複製程式碼

MvpFragment

通過泛型傳入Presenter,並覆寫BaseView中的setPresenter

public class MvpFragment <P extends BasePresenter> extends Fragment implements BaseView<P>{
    public P mPresenter;

    @Override
    public void setPresenter(BasePresenter presenter) {
        if (presenter != null)
            mPresenter = (P) presenter;
    }
}複製程式碼

MvpPresenter

通過泛型傳入BaseView,實現setView方法

public class MvpPresenter <V extends BaseView> {
    public Context context;
    public V mView;

    public void setView(Context context, V mView) {
        this.context = context;
        this.mView = mView;
        mView.setPresenter(this);
    }

}複製程式碼

小結

通過以上方法,Activity中呼叫mapPresenter.setView(this, mapFragment)將view傳入,而在setView中又呼叫setPresenter將view和Presenter繫結,於是可以在view層和presenter呼叫相關介面。

相關文章