Introduction to the

Encapsulate MvpFragment and MvpPresenter to simplify MVP construction and achieve lazy purposes. Refer to another previous article: Android Mvp Practices

Final effect

Fragment and Presenter can be bound by inheriting MvpFragmen and MvpPresenter, respectively.

Activity

The Activity container contains two fragments and binds them to the present in the Activity.

@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(a) {
        setDefaultFragment();
        mapPresenter.setView(this, mapFragment); // View is bound to presenter
        userPresenter.setView(this, userFragment);// View is bound to presenter
    }

    private void setDefaultFragment(a) {
        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()) {// Check to see if it has been added
                transaction.hide(from).add(R.id.fragcontent, to).commit(); // Hide the current fragment and add the next fragment to the Activity
            } else {
                transaction.hide(from).show(to).commit(); // Hide the current fragment and display the next fragment}}}// Click to switch fragment slightly..
}Copy the code

Contract

Presenter and View interfaces

public class MapContract {

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

    public interface Presenter extends BasePresenter {
     / /...}}Copy the code

The View layer

@EFragment(R.layout.fragment_map)
public class MapFragment extends MvpFragment<MapContract.Presenter> implements MapContract.View{
   / /... Implement methods in the interface
}Copy the code

Presenter layer

@EBean
public class MapPresenter extends MvpPresenter<MapContract.View> implements MapContract.Presenter {
   / /... Implement methods in the interface
}Copy the code

This simply associates the Presenter layer with the View and handles different business in each layer.

The implementation process

The base class

public interface BaseView<T> {
    void setPresenter(T presenter);
}Copy the code
public interface BasePresenter {}Copy the code

MvpFragment

Presenter is passed in via generics and setPresenter in BaseView is overridden

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; }}Copy the code

MvpPresenter

The setView method is implemented by passing in the BaseView via generics

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); }}Copy the code

summary

SetView (this, mapFragment) is called to pass in a view, and setView is called to bind a view to a Presenter. The relevant interfaces can then be called from the View layer and presenter.