mvp,activity,fragment抽取

1.view层

public interface BaseView {
}

2.model层

public abstract class BaseModel {
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    public void onDestroy() {
        compositeDisposable.clear();
    }
}

3.presenter层

public abstract class BasePresenter {
    protected ArrayList models = new ArrayList<>();
    protected V iView;

    public void attch(V v) {
        iView = v;
    }

    public BasePresenter() {
        initModel();
    }

    protected abstract void initModel();


    public void onDestroy() {
        iView = null;
        if (models.size()>0){
            for (BaseModel model:models) {
                model.onDestroy();
            }
        }
    }
}

4.fragment层

public abstract class BaseFragment extends Fragment {

    protected T presenter;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), null);
        unbinder = ButterKnife.bind(this, view);
        presenter = initPresenter();
        if (presenter != null){
            presenter.attachent((V)this);
        }
        initView(view);
        initListener();
        initData();
        return view;
    }

    protected void initData() {

    }

    protected void initListener() {

    }

    protected void initView(View view) {
    }

    protected abstract T initPresenter();

    protected abstract int getLayoutId();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if(presenter != null){
            presenter.onDestory();
        }
    }

}

5.activity层

public abstract class BaseActivity extends AppCompatActivity{
    protected T presenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        presenter = initPresenter();
        if (presenter != null){
            presenter.attch((V)this);
        }
        initListener();
        initView();
        initData();
    }

    protected void initData() {

    }

    protected void initView() {

    }

    protected void initListener() {

    }

    protected abstract T initPresenter();

    protected abstract int getLayoutId();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}

你可能感兴趣的:(mvp,activity,fragment抽取)