Giter Site home page Giter Site logo

mvp's Introduction

Mvp

Release

Deprecated using mvvm instead

用于快速引入 MVP 框架至 Android 应用开发中

添加依赖

  1. 添加 jitpack 仓库至根目录下的build.gradle文件中
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. 添加编译依赖
dependencies {
    compile 'me.alzz:mvp:1.0.3'
}

快速使用

  1. 定义 view 接口
public interface IMainView extends IView {
    void showMsg(String msg);
}
  1. 定义 presenter 接口(可选)
public interface IMainPresenter extends IPresenter {
    /**
     * 从服务器读取消息
     */
    void queryMsg();
}
  1. 继承 BasePresenter 实现 presenter
public class MainPresenter extends BasePresenter<IMainView> implements IMainPresenter {

    public void queryMsg() {
        // may be query from db or network
        String msg = "msg from query";
        // ... 

        mView.showMsg(msg);
    }
}
  1. 继承BaseMvpActivity 并实现 view 接口。可考虑由基类继承 BaseMvpActivity
public class MainActivity extends BaseMvpActivity implements IMainView {

    private TextView mMsgTv;

    // 只需要声明,不需要赋值
    MainPresenter mMainPresenter;
    
    // 或者使用接口,实现类可放至单独的 module 中,编译时引用
    // IMainPresenter mMainPresenter;
    
    // 如果自己赋值也是可以的
    // MainPresenter mMainPresenter = new MainPresenter();
    // IMainPresenter mMainPresenter = new MainPresenter();


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

        mMsgTv = (TextView) findViewById(R.id.msg_tv);

        mMainPresenter.queryMsg();
    }

    @Override
    public void showMsg(String msg) {
        mMsgTv.setText(msg);
    }
}

Proguard

# 保留presenter默认构造函数
-keepclasseswithmembers class * extends me.alzz.mvp.BasePresenter {
    public <init>();
}

致谢

  • dexmaker:A utility for doing compile or runtime code generation targeting Android's Dalvik VM

mvp's People

Contributors

alvminvm avatar

Stargazers

 avatar  avatar Li. avatar

Watchers

James Cloos avatar  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.