mvp,activity,fragment抽取

2019-03-18  本文已影响0人  初昇陌

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<V extends BaseView> {
    protected ArrayList<BaseModel> 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<V extends BaseView,T extends BasePresenter> 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<V extends BaseView,T extends BasePresenter> 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();
    }
}
上一篇下一篇

猜你喜欢

热点阅读