Mvp_抽取

作者: 玖玖君 | 来源:发表于2019-07-01 16:25 被阅读0次

    1.BaseActivity层

    public abstract class BaseActivity<P extends BasePresenter,M extends BaseModel,V extends BaseView> extends AppCompatActivity {
        protected P presenter;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getLayoutId());
            ButterKnife.bind(this);
            presenter=initMvpPresenter();
            if (presenter!=null){
                presenter.setBaseModel(initMvpModel());
                presenter.setBaseView(initMvpView());
            }
            initView();
            initData();
            initListener();
        }
    
        protected abstract V initMvpView();
    
        protected abstract M initMvpModel();
    
    
        protected abstract P initMvpPresenter();
    
        private void initView() {
    
        }
    
        private void initData() {
    
        }
    
        private void initListener() {
    
        }
    
        protected abstract int getLayoutId();
    }
    

    2.BaseCallBack层(与BaseView层相同)

    public interface BaseCallBack<T,M> {
        void onSuccess(T t);
        void onFail(M m);
    }
    

    3.Baseview层

    public interface  BaseView<T,M> {
        void onSuccess(T t);
        void onFail(M m);
    }
    
    

    4.Basemodel层

     空空空···············
    

    5.Basepresenter层

    public abstract class BasePresenter<M extends BaseModel,V extends BaseView> {
        protected M baseModel;
        protected V baseView;
    
        public void setBaseModel(M baseModel) {
            this.baseModel = baseModel;
        }
    
        public void setBaseView(V baseView) {
            this.baseView = baseView;
        }
    }
    
    

    6.本地调用Basemodel

    public class FoodModel extends BaseModel {
        public void FoodData(final BaseCallBack<Foodbean,String> callBack){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(FoodServer.baseurl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            FoodServer foodServer = retrofit.create(FoodServer.class);
            Observable<Foodbean> observable = foodServer.getFood();
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Foodbean>() {
                        @Override
                        public void onSubscribe(Disposable d) {
    
                        }
    
                        @Override
                        public void onNext(Foodbean foodbean) {
                        callBack.onSuccess(foodbean);
                        }
    
                        @Override
                        public void onError(Throwable e) {
                        callBack.onFail("网络错误:"+e.getMessage());
                        }
    
                        @Override
                        public void onComplete() {
    
                        }
                    });
        }
    }
    

    7.本地调用工具BasePresenter

    public class FoodPresenter extends BasePresenter<FoodModel, BaseView> implements BaseCallBack<Foodbean, String> {
        public void FoodData(){
            if (model!=null){
                model.FoodData(this);
            }
        }
    
        @Override
        public void onSuccess(Foodbean foodbean) {
            if (view!=null){
                view.onSuccess(foodbean);
            }
        }
    
        @Override
        public void onFail(String s) {
            if (view!=null){
                view.onFail(s);
            }
        }
    }
    
    

    7.activity层(注意重写onPostCreate)

     
    public class MainActivity extends BaseActivity<FoodPresenter, FoodModel, BaseView<Foodbean, String>> implements BaseView<Foodbean, String> {
    
        @BindView(R.id.btn_jiexi)
        RecyclerView recy;
        private ArrayList<Foodbean.DataBean> list;
        private RecyAdapter adapter;
    
        @Override
        protected void onPostCreate(@Nullable Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            initData();
        }
    
        @Override
        protected BaseView<Foodbean, String> initMvpView() {
            return this;
        }
    
        @Override
        protected FoodModel initMvpModel() {
            return new FoodModel();
        }
    
        @Override
        protected FoodPresenter initMvpPresenter() {
            return new FoodPresenter();
        }
    
        @Override
        protected int getLayoutId() {
            return R.layout.activity_main;
        }
    
        protected void initData() {
            presenter.FoodData();
            list = new ArrayList<>();
            adapter = new RecyAdapter(MainActivity.this, list);
            recy.setLayoutManager(new LinearLayoutManager(this));
            recy.addItemDecoration(new DividerItemDecoration(this, 1));
            recy.setAdapter(adapter);
        }
    
        @Override
        public void onSuccess(Foodbean foodbean) {
            List<Foodbean.DataBean> data = foodbean.getData();
            list.addAll(data);
            adapter.notifyDataSetChanged();
        }
    
        @Override
        public void onFail(String s) {
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        }
    }
    
    

    8.Basefragment层

    public abstract class BaseFragment extends Fragment {
    
        private Unbinder unbinder;
        protected Context mContext;
    
        @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);
    
            initMvp();
            initView();
            initData();
            initListener();
            return view;
        }
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mContext = context;
        }
    
        protected void initListener() {
    
        }
    
        protected void initData() {
    
        }
    
        protected void initView() {
        }
    
        protected void initMvp() {
    
        }
    
        protected abstract int getLayoutId();
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            unbinder.unbind();
        }
    }
    

    9.BaseMvpFragment(继承BaseFragment)

    
    public abstract class BaseMvpFargment<P extends BasePresenter, M extends BaseModel, V extends BaseView> extends BaseFragment {
        protected P presenter;
    
        @Override
        protected void initMvp() {
            super.initMvp();
            presenter = initMvpPresenter();
            if (presenter != null) {
                presenter.setModel(initMvpModel());
                presenter.setView(initMvpView());
            }
        }
    
        protected abstract V initMvpView();
    
        protected abstract M initMvpModel();
    
        protected abstract P initMvpPresenter();
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            presenter.destroy();
        }
    }
    
    
    

    10.调用BaseMvpFragment

    public class Zhi_OneFragment extends BaseMvpActivity<NewsPresenter, NewsModel, NewsView> implements NewsView, OnRefreshListener {
    
    
        @BindView(R.id.recy)
        RecyclerView recy;
        @BindView(R.id.smart)
        SmartRefreshLayout smart;
        private RecyNewsAdapter adapter;
        private ArrayList<NewsBean.StoriesBean> storiesBeans;
        private ArrayList<NewsBean.TopStoriesBean> topStoriesBeans;
    
        public Zhi_OneFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void initView() {
            storiesBeans = new ArrayList<>();
            topStoriesBeans = new ArrayList<>();
            recy.setLayoutManager(new LinearLayoutManager(getActivity()));
            adapter = new RecyNewsAdapter(getActivity(), storiesBeans, topStoriesBeans);
            recy.setAdapter(adapter);
    
            smart.setOnRefreshListener(this);
            adapter.setOnItemClickListener(new RecyNewsAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View v, NewsBean.StoriesBean pos) {
                    ToastUtil.showShort(pos.getTitle());
                }
            });
        }
        @Override
        public void initData() {
            if (presenter != null) {
                presenter.getNews();
            }
        }
        @Override
        protected NewsView initMvpView() {
            return this;
        }
    
        @Override
        protected NewsModel initMvpModel() {
            return new NewsModel();
        }
    
        @Override
        protected NewsPresenter initMvpPresenter() {
            return new NewsPresenter();
        }
    
        @Override
        protected int getLayoutId() {
            return R.layout.fragment_zhi__one;
        }
    
        @Override
        public void onSuccess(NewsBean newsBean) {
            adapter.setData(newsBean);
            smart.finishRefresh();
        }
    
        @Override
        public void onFail(String v) {
            ToastUtil.showLong(v);
        }
    
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            initData();
        }
    }
    

    应用崩溃问题解决

    背景:在发动网络请求的时候,退出当前activity

    冲突:此时如果回到主线程更新UI,APP会崩溃

    解决:当activity退出时,调用DisPosable.dispose()切断观察者和被观察者的连接,使得观察者无法收到事件&响应事件;当出现多个disposable时,采用Rxjava内置容器CompositeDisposable进行统一管理

    CompositeDisposable compositeDisposable = new CompositeDisposable();
    //添加disposable到CompositeDisposable容器
    compositeDisposable .add();
    //清空CompositeDisposable容器,在父类的baseModel中调用
    compositeDisposable .clear();
    

    · 在BaseModel中

    public abstract class BaseModel {
        
        //用于统一切断观察者与被观察者关系
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    
        //切断,P层调用
        public void destroy() {
            compositeDisposable.clear();
        }
    }
    

    在basePresenter中

    public class BasePresenter<V extends BaseView,M extends BaseModel>{  
    
        //创建一个集合,用于存放model
    protected ArrayList<BaseModel> list = new ArrayList<BaseModel>();
    
        protected V view;
        protected M model;
    
        public void addView(V v) {
            this.view = v;
        }
    
        public void addModel(M m) {
            this.model = m;
            //添加model
            list.add(m);
        }
    
        public void destroy() {
            //如果view不为空,页面关闭,强制置空,否则内存泄漏
            if (view != null) {
                view = null;
            }
    
            //如果页面关闭,切断网络请求的观察者与被观察者的关系
            if (list.size() > 0) {
                for (BaseModel baseModel : list) {
                    baseModel.destroy();
                }
            }
    
            //如果model不为空,置空
            if (model != null) {
                model = null;
            }
        }
    }
    

    在BaseMVPActivity中

     /**
         * 页面关闭时
         * 切断连接
         * 内存回收
         */
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (presenter != null) {
                presenter.destroy();
            }
        }
    
    

    相关文章

      网友评论

        本文标题:Mvp_抽取

        本文链接:https://www.haomeiwen.com/subject/hbxicctx.html