Giter Site home page Giter Site logo

mvp's Introduction

MVP

MVP+Rxjava+Retrofit+ViewBinding+EventBus

基础架构图:

基础架构图

  • 1.本框架将ViewBinding进行了封装,使用时只需传入对应ViewBinding,即可通过vb.控件id访问;
  • 2.本框架对Presenter进行了封装,使用时只需通过注解的方式@InjectPresenter引入presenter即可,并且不再需要注册和注销操作,支持一对多;
  • 3.本框架对EventBus进行了封装,不再需要注册和注销操作;

效果图

以上具体使用方法参考源码或下方案例:

1.Activity:

public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements LoginMvpView {

    @InjectPresenter
    LoginPresenter loginPresenter;

    @Override
    public void initView() {

    }

    @SuppressLint("CheckResult")
    @Override
    public void initClick() {
        RxView.clicks(vb.btnLogin).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
            if (!TextUtils.isEmpty(vb.etAccount.getText().toString()) && !TextUtils.isEmpty(vb.etPwd.getText().toString())) {
                showLoading();
                loginPresenter.login(vb.etAccount.getText().toString(), vb.etPwd.getText().toString());
            } else {
                Toast.makeText(mContext, "请输入账号和密码", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void loginSuccess(UserModel userBean) {
        dismissLoading();
        startActivity(new Intent(mContext, MainActivity.class));
    }

    @Override
    public void loginFail(int error_code, String error_msg) {
        dismissLoading();
        startActivity(new Intent(mContext, MainActivity.class));//请求肯定是失败的,这里只做演示
    }

    /**
     * 接收消息
     *
     * @param eventMsg
     */
    @Override
    public void handleEventMsg(EventMsg eventMsg) {
        super.handleEventMsg(eventMsg);
        if (eventMsg.code == MsgCode.LOGIN_SUCCESS) {
            Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

}

2.Fragment同

3.Adapter:

public class ListAdapter extends BaseAdapter<ItemListBinding, String> {

    public ListAdapter(Activity context, List<String> listData) {
        super(context, listData);
    }

    public void convert(ItemListBinding vb, String t, int position) {
        vb.tvContent.setText(t);
    }

}

4.发送/接收消息:

 App.post(new EventMsg(MsgCode.LOGIN_SUCCESS));
 
 /**
 * 接收消息
 *
 * @param eventMsg
 */
@Override
public void handleEventMsg(EventMsg eventMsg) {
    super.handleEventMsg(eventMsg);
    if (eventMsg.code == MsgCode.LOGIN_SUCCESS) {
        Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
        finish();
    }
}

对RxView的防重点击可以自定义简化:

public class RxView {

    //默认点击时间间隔为1秒
    @SuppressLint("CheckResult")
    public static void clicks(View view, View.OnClickListener onClickListener) {
        com.jakewharton.rxbinding3.view.RxView.clicks(view).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
            onClickListener.onClick(view);
        });
    }

    @SuppressLint("CheckResult")
    public static void clicks(View view, int duration, View.OnClickListener onClickListener) {
        com.jakewharton.rxbinding3.view.RxView.clicks(view).throttleFirst(duration, TimeUnit.SECONDS).subscribe(v -> {
            onClickListener.onClick(view);
        });
    }
}

使用方法:

 RxView.clicks(vb.flBack, v -> finish());

mvp's People

Contributors

baiyuliang avatar

Stargazers

 avatar liwq avatar  avatar  avatar  avatar XX_Code avatar wang avatar Luke Wong avatar ysx avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.