zoukankan      html  css  js  c++  java
  • android之MVP笔记入门

    谈起MVP,有点惭愧,还是等到面试的时候被人问到才去做了解,自己阅读了网上的一部分MVP文档,感觉每个人都是按照自己的理解方式来写自己的博客,自己也写个自己的心得体会。

    网上的图一大堆,这里就截取一个给大家看。

    大神鸿杨的总结就是:Model与View之间的交互由Presenter完成。还有一点就是Presenter与View之间的交互是通过接口的(代码中会体现)。

    谈起例子也是各有不同。

    下面是我的例子,首先给大家看一下结构:

    对应的类如下:

    TestPresentBean
     1 public class TestPresentBean {
     2     public String name;
     3     public String password;
     4 
     5     public String getName() {
     6         return name;
     7     }
     8 
     9     public void setName(String name) {
    10         this.name = name;
    11     }
    12 
    13     public String getPassword() {
    14         return password;
    15     }
    16 
    17     public void setPassword(String password) {
    18         this.password = password;
    19     }
    20 }
    ITestPresentView
    public interface ITestPresentView {
    
        void findView();
        void showLoading();
        void dismissLoading();
        void showUserInfo(TestPresentBean bean);
    
    }
    TestPresentModel
    public class TestPresentModel {
        public void loadData(final String name, final String pass, final ListenCallBack listenCallBack){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    TestPresentBean bean = new TestPresentBean();
                    bean.setName(name);
                    bean.setPassword(pass);
                    listenCallBack.loadSuccess(bean);
                }
            }).start();
        }
    
    }
    TestPresenter
    public class TestPresenter {
    
        Handler mHandler = new Handler();
    
        ITestPresentView iTestPresentView;
        TestPresentModel testPresentbiz;
    
        public TestPresenter(ITestPresentView iTestPresentView) {
            this.iTestPresentView = iTestPresentView;
            testPresentbiz = new TestPresentModel();
        }
    
        public void loadData(){
            iTestPresentView.showLoading();
            testPresentbiz.loadData("张三", "123", new ListenCallBack() {
                @Override
                public void loadSuccess(final TestPresentBean testPresentBean) {
                    //需要在UI线程执行
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            iTestPresentView.showUserInfo(testPresentBean);
                            iTestPresentView.dismissLoading();
                        }
                    });
    
                }
    
                @Override
                public void loadFailed() {
                    //需要在UI线程执行
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            iTestPresentView.dismissLoading();
                        }
                    });
                }
            });
        }
    
    }
    TestPresentActivity
    public class TestPresentActivity extends AppCompatActivity implements ITestPresentView {
    
        TestPresenter presenter;
        TextView mTestView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_present);
            presenter = new TestPresenter(this);
            initView();
            initData();
        }
        private void initView() {
            mTestView = (TextView) findViewById(R.id.txt_show);
        }
    
        private void initData() {
            TestPresenter presenter = new TestPresenter(this);
            presenter.loadData();
        }
    
        @Override
        public void findView() {
    
        }
    
        @Override
        public void showLoading() {
            Toast.makeText(this, "正在加载", Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void dismissLoading() {
            Toast.makeText(this, "加载完成", Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void showUserInfo(TestPresentBean bean) {
            mTestView.setText(bean.getName()+":"+bean.getPassword());
        }
    
    
    }

    每个人有自己的用法,希望这些跟您自己的代码作对比,写出更规范的MVP模式。

    这里给大家提供一些参考网站:

    http://www.jianshu.com/p/14283d8d3a60

    http://www.jianshu.com/p/9a6845b26856

    http://blog.csdn.net/lmj623565791/article/details/46596109

    http://blog.csdn.net/hanchendong/article/details/61919599



  • 相关阅读:
    dijkstra (模板)
    LUOGU P2476 [SCOI2008]着色方案
    LUOGU P2344 奶牛抗议 (树状数组优化dp)
    LaTeX 公式大全
    LUOGU P1505 [国家集训队]旅游 (树链剖分+线段树)
    LUOGU P2416 泡芙 (缩点+树剖)
    LUGOU P1092 虫食算
    BZOJ 3090: Coci2009 [podjela] (树形背包)
    bzoj 4823 [Cqoi2017]老C的方块——网络流
    bzoj 3158 千钧一发——网络流
  • 原文地址:https://www.cnblogs.com/bxfx111/p/6830127.html
Copyright © 2011-2022 走看看