Android知识点总结(十六) Android MVP

相关文章:

Android 知识点总结(目录) https://blog.csdn.net/a136447572/article/details/81027701

Android知识点总结(十五) Android MVP 简易模型 https://blog.csdn.net/a136447572/article/details/82593857

BaseActivity

public class BaseActivity extends Activity implements IBaseView {

    private ProgressDialog mProgressDialog ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMessage("数据请求中");
    }
    @Override
    public void showLoading() {
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }
    @Override
    public void hideLoading() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
    @Override
    public void showError() {
        showToast("网络异常");
    }
    @Override
    public void showToast(String toastMsg) {
        Toast.makeText(this, toastMsg, Toast.LENGTH_SHORT).show();
    }
    @Override
    public Context getContent() {
        return BaseActivity.this;
    }

BasePresenter

public class BasePresenter<V extends  IBaseView> {

    private V mvpView ;

    public void attachView(V mvpView){
        this.mvpView = mvpView;
    }

    public void detachView(){
        this.mvpView = null ;
    }

    public boolean isViewAttached(){
        return mvpView !=null ;
    }

    public V getView(){
        return mvpView ;
    }
}

Callback

public interface Callback {

    /**
     * 数据请求成功
     * @param data 请求到的数据
     */
    void onSuccess(T data);
    /**
     *  使用网络API接口请求方式时,虽然已经请求成功但是由
     *  于{@code msg}的原因无法正常返回数据。
     */
    void onFailure(String msg);
    /**
     * 请求数据失败,指在请求网络API接口请求方式时,出现无法联网、
     * 缺少权限,内存泄露等原因导致无法连接到请求数据源。
     */
    void onError();
    /**
     * 当请求数据结束时,无论请求结果是成功,失败或是抛出异常都会执行此方法给用户做处理,通常做网络
     * 请求时可以在此处隐藏“正在加载”的等待控件
     */
    void onComplete();

IBaseView


public interface IBaseView {

    /**
     * 显示正在加载进度框
     */
    void showLoading();
    /**
     * 隐藏正在加载进度框
     */
    void hideLoading();
    /**
     * 当数据请求异常,调用此接口提示
     */
    void showError();

    void showToast(String toastMsg);

    Context getContent();
}

MainActivity

public class MainActivity extends BaseActivity implements MvpView {

    TextView text ;
    MvpPresenter presenter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.text);

        presenter = new MvpPresenter();
        presenter.attachView(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
    // button 点击事件调用方法
    public void getData(View view){
        presenter.getData("normal");
    }
    // button 点击事件调用方法
    public void getDataForFailure(View view){
        presenter.getData("failure");
    }
    // button 点击事件调用方法
    public void getDataForError(View view){
        presenter.getData("error");
    }


    @Override
    public void showData(String data) {
        text.setText(data);
    }
}

MvpModel

public class MvpModel {

    /**
     * 获取网络接口数据
     * @param param 请求参数
     * @param callback 数据回调接口
     */
    public static void getNetData(final String param, final Callback callback){
        // 利用postDelayed方法模拟网络请求数据的耗时操作
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                switch (param){
                    case "normal":
                        callback.onSuccess("根据参数"+param+"的请求网络数据成功");
                        break;
                    case "failure":
                        callback.onFailure("请求失败:参数有误");
                        break;
                    case "error":
                        callback.onError();
                        break;
                }
                callback.onComplete();
            }
        },2000);
    }
}

MvpPresenter

public class MvpPresenter extends BasePresenter {

    public void getData(String params){
        if (!isViewAttached()){
            return ;
        }
        getView().showLoading();
        MvpModel.getNetData(params, new Callback() {
            @Override
            public void onSuccess(String data) {
                if (isViewAttached()){
                    getView().showData(data);
                }
            }
            @Override
            public void onFailure(String msg) {
                if (isViewAttached()){
                    getView().showToast(msg);
                }
            }
            @Override
            public void onError() {
                if (isViewAttached()){
                    getView().showError();
                }
            }
            @Override
            public void onComplete() {
                if (isViewAttached()){
                    getView().hideLoading();
                }
            }
        });
    }
}

MvpView

public interface MvpView extends IBaseView {

    void showData(String data);
}

你可能感兴趣的:(andriod)