fragment(base)抽基类

//需要的依赖


//ok
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
//gson
implementation 'com.google.code.gson:gson:2.8.4'
//拦截器的依赖
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
//reclerview的依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
//xRecyclerView的依赖
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
implementation 'com.android.support:design:27.1.1'
//插件依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
implementation files('libs/universal-image-loader-1.9.5.jar')

//用到的接口

//商品分类接口
public static final String URL_CLASS_ONE="https://www.zhaoapi.cn/product/getCatagory";
public static final String URL_CLASS_TWO="https://www.zhaoapi.cn/product/getProductCatagory";

//购物车接口
public static final String  URL_CART="https://www.zhaoapi.cn/product/getCarts?uid=71";

1.先自定义一个接口

public interface IView {
}

2.BasePresenter

public abstract class BasePresenter<V extends IView> {

    protected V view;

    public BasePresenter(V view) {
        this.view = view;
        initModel();
    }

    protected abstract void initModel();

    //解决内存泄漏
    void onDestory() {
        view = null;
    }
}

//BaseFragment

public abstract class BaseFragment <P extends BasePresenter> extends Fragment{

    protected  P presenter;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(provId(),null);
        presenter=provide();
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initViews(view);
        initListener();
        initData();
    }

    protected abstract void initData();

    protected abstract void initListener();

    protected abstract void initViews(View view);

    protected abstract int provId();
    protected abstract P provide();
}

//分类的model层(网络请求)

public class ClassModel {


    public void doNetLeft(final onLeftCallBak onLeftCallBak) {

        String url = HttpConfig.URL_CLASS_ONE;
        OkHttpUtils.getInstance().doGet(url, new OkHttpUtils.onCallBack() {
            @Override
            public void onFaild(Exception e) {
                if (onLeftCallBak != null) {
                    onLeftCallBak.onLeftFaild("有错");
                }
            }

            @Override
            public void onResponse(String json) {

                Gson gson = new Gson();
                LeftBean leftBean = gson.fromJson(json, LeftBean.class);
                String code = leftBean.getCode();
                if ("0".equalsIgnoreCase(code)) {
                    if (onLeftCallBak != null) {
                        onLeftCallBak.onLeftSuccess(leftBean);
                    }
                } else {
                    if (onLeftCallBak != null) {
                        onLeftCallBak.onLeftFaild("有错");
                    }
                }
            }
        });
    }

    public void doNetRight(int cid, final onRightCallBack onRightCallBack) {
        String url = HttpConfig.URL_CLASS_TWO+"?cid="+cid;

        OkHttpUtils.getInstance().doGet(url, new OkHttpUtils.onCallBack() {
            @Override
            public void onFaild(Exception e) {
                if (onRightCallBack != null) {
                    onRightCallBack.onRightFaild("有误");
                }
            }

            @Override
            public void onResponse(String json) {

                Gson gson = new Gson();
                RightBean rightBean = gson.fromJson(json, RightBean.class);
                String code = rightBean.getCode();
                if ("0".equalsIgnoreCase(code)) {
                    if (onRightCallBack != null) {
                        onRightCallBack.onRightSuccess(rightBean);
                    }
                } else {
                    if (onRightCallBack != null) {
                        onRightCallBack.onRightFaild("有错");
                    }
                }
            }
        });
    }

    public interface onLeftCallBak {
        void onLeftSuccess(LeftBean leftBean);

        void onLeftFaild(String error);
    }

    public interface onRightCallBack {
        void onRightSuccess(RightBean rightBean);

        void onRightFaild(String error);
    }

}

//分类的presenter

public class ClassesPresenter extends BasePresenter<ClassesView> {


    private ClassModel classModel;

    public ClassesPresenter(ClassesView view) {
        super(view);
    }

    @Override
    protected void initModel() {
        classModel = new ClassModel();
    }

    public void getLeft() {
        classModel.doNetLeft(new ClassModel.onLeftCallBak() {
            @Override
            public void onLeftSuccess(LeftBean leftBean) {
                if (view != null) {
                    view.onLeftSuccess(leftBean);
                }
            }

            @Override
            public void onLeftFaild(String error) {

                if (view != null) {
                    view.onLeftFaild(error);
                }
            }
        });
    }


    public void getRight(int cid) {
        classModel.doNetRight(cid,new ClassModel.onRightCallBack() {
            @Override
            public void onRightSuccess(RightBean rightBean) {
                if (view != null) {
                    view.onRightSuccess(rightBean);
                }
            }

            @Override
            public void onRightFaild(String error) {

                if (view != null) {
                    view.onRightFaild(error);
                }
            }
        });
    }
}

//分类的View

public interface ClassesView extends IView {

    void onLeftSuccess(LeftBean leftBean);

    void onLeftFaild(String error);

    void onRightSuccess(RightBean rightBean);

    void onRightFaild(String error);

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值