关闭

android MVP

标签: android开发android studiomvpandroid
157人阅读 评论(0) 收藏 举报
分类:
1、public interface BaseView<T> {

    void showToast(String msg);


    /**
     * 网络错误
     */
    void networkError();

}
<pre name="code" class="java">2、public class BankInfoContract {
    public interface View extends BaseView<Presenter> {
        void getBinkInfoSuccess(List<BankInfo> bankInfos);
    }

    public interface Presenter extends BasePresenter {
        /**
         * 银行列表
         *
         * @param type:开户类型 type=qiluce,tjpme
         */
        void getBinkInfo(String type);

    }
}
<pre name="code" class="java">3、public class BankInfoPresenter implements BankInfoContract.Presenter {

    private BankInfoContract.View view;

    public BankInfoPresenter(BankInfoContract.View view) {
        this.view = view;
    }

    @Override
    public void getBinkInfo(String type) {
        ApiKaiHu.getBankInfo(type, new IHttpCallBack() {
            @Override
            public void onSuccess(BaseInfo base) {
                if (null == view) {
                    return;
                }
                List<BankInfo> bankInfos = JsonUtils.getArray(base.value, BankInfo.class);
                view.getBinkInfoSuccess(bankInfos);
            }

            @Override
            public void onFailure(int code, String msg) {
                if (null == view) {
                    return;
                }
                if (404 == code) {
                    view.networkError();
                } else {
                    view.showToast(msg);
                }

            }
        });
    }

    @Override
    public void start() {

    }

    @Override
    public void onDestroy() {
        view = null;
    }
}
4、<span style="font-family: Arial, Helvetica, sans-serif;">@SuppressLint("ValidFragment")</span><pre name="code" class="java">public class BankInfoFragment extends BaseFragment implements BankInfoContract.View, View.OnClickListener {
    private SwitchFragmentInterface mCallBack;
    private BankInfoPresenter mPresenter;
    private TextView tv_bank_name;
    private RelativeLayout rl_select_bank;
    private EditText et_bank_num;
    private ImageView iv_see_bank_name;
    private TextView tv_bank_info_next;
    private List<String> bankNames;
    private String type;
    private List<BankInfo> bankInfos;
    private boolean showOrHidePasswd = false;


    public BankInfoFragment(SwitchFragmentInterface CallBack) {
        this.mCallBack = CallBack;
    }

    @Override
    public String getTitle() {
        return "银行信息";
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_backinfo;
    }

    @Override
    protected void initView() {
        tv_bank_name = (TextView) findViewById(R.id.tv_bank_name);
        rl_select_bank = (RelativeLayout) findViewById(R.id.rl_select_bank);
        et_bank_num = (EditText) findViewById(R.id.et_bank_num);
        iv_see_bank_name = (ImageView) findViewById(R.id.iv_see_bank_num);
        tv_bank_info_next = (TextView) findViewById(R.id.tv_bank_info_next);
    }

    @Override
    protected void initData() {
        bankNames = new ArrayList<>();
        type = getArguments().getString("type");
        mPresenter = new BankInfoPresenter(this);
        mPresenter.getBinkInfo(type);
    }

    @Override
    protected void initListener() {
        tv_bank_info_next.setOnClickListener(this);
        rl_select_bank.setOnClickListener(this);
        iv_see_bank_name.setOnClickListener(this);
        et_bank_num.addTextChangedListener(mBankWatcher);
    }

    @Override
    public void getBinkInfoSuccess(List<BankInfo> bankInfos) {
        this.bankInfos = bankInfos;
        for (int i = 0; i < bankInfos.size(); i++) {
            bankNames.add(bankInfos.get(i).name);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_bank_info_next:
                addBankInfoToParams();
                mCallBack.onNext(this);
                break;
            case R.id.rl_select_bank:
                new SelecBankDialog(getContext(), bankNames, new SelecBankDialog.OnCompleteListener() {
                    @Override
                    public void onComplete(String bankName) {
                        tv_bank_name.setText(bankName);
                    }
                }).show();
                break;
            case R.id.iv_see_bank_num:
                showOrHidePasswd();
                break;
        }
    }

    private void showOrHidePasswd() {
        if (showOrHidePasswd) {
            //设置EditText文本为可见的
            et_bank_num.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            //设置EditText文本为隐藏的
            et_bank_num.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        showOrHidePasswd = !showOrHidePasswd;
        et_bank_num.postInvalidate();
        //切换后将EditText光标置于末尾
        CharSequence charSequence = et_bank_num.getText();
        if (charSequence instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence;
            Selection.setSelection(spanText, charSequence.length());
        }
    }

    private void addBankInfoToParams() {
        String bankName = tv_bank_name.getText().toString();
        String bankNum = et_bank_num.getText().toString();
        for (int i = 0; i < bankInfos.size(); i++) {
            if (bankName.equals(bankInfos.get(i).name)) {
                ((KaiHuActivity) getActivity()).getParamsUtils().addBodyParameter("bankId", bankInfos.get(i).value);
//                if ("qiluce".equals(type)) {
//                    ((KaiHuActivity) getActivity()).getParamsUtils().addBodyParameter("account", bankNum);
//                } else {
                ((KaiHuActivity) getActivity()).getParamsUtils().addBodyParameter("bankAcc", bankNum);
//                }
                break;
            }
        }
    }

    /**
     * 对手机号输入内容监听 并改变获取验证码按钮状态
     */
    private TextWatcher mBankWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //校验银行账号长度为>=16&&银行名字不为空时获取验证码按钮可点击
            if (et_bank_num.length() >= 16 && tv_bank_name.length() != 0) {
                tv_bank_info_next.setClickable(true);
                tv_bank_info_next.setBackgroundResource(R.drawable.bt_bg_shape_clickable);
            } else {
                tv_bank_info_next.setClickable(false);
                tv_bank_info_next.setBackgroundResource(R.drawable.bt_bg_shape_unclickable);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };


    @Override
    public void showToast(String msg) {
        ToastUtils.show(msg);
    }

    @Override
    public void networkError() {
        ToastUtils.show("网络错误");
    }
}









0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9865次
    • 积分:372
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:12篇
    • 译文:0篇
    • 评论:1条