美文网首页
Android MVP

Android MVP

作者: lsyt0309 | 来源:发表于2018-09-27 15:14 被阅读0次

    V层

    /**
     * Activity UI 更新接口示例
     */
    public interface MainPageView{
    
        void showToast(String msg);
    
    }
    

    P层

    /**
     * Presenter抽象接口
     */
    public interface Presenter<T> {
    
        void attachView(T view);
    
        void detachView();
    }
    

    M层

    /**
     * 数据转换器
     */
    public interface MainInfoInteractor{
      
        /**
         * 回调数据回传至P层
         */
        static interface Callback{
          void onResult(String json);
        }
    
        void loadData(String requestParamet, Callback callback);
        
        void loadData(String requestParamet);
    }
    

    V层接口实现

    在Activity/Fragment中实现,并获取Peraenter对象
    关系流程:V-->P

    public class MainActivity extends AppCompatActivity implements MainPageView{
          
          private MainPresenter mPresenter;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mPresenter = new MainPresenter(this);
            mPresenter.attachView(this);
        }
    
            @Override
        protected void onDestroy() {
            super.onDestroy();
            mPresenter.detachView();
        }
    
        @Override
        public void showToast(String msg) {
           ...
        }
    }
    

    P层接口实现

    实现Presenter接口与M中的Callback接口,并接收View对象
    关系流程:V-->P-->M-->P-->V

    public class MainPresenter implements Presenter<MainPageView>, MainInfoInteractor.Callback{
    
      private Context mContext;
    
      private MainPageView mView;  
    
      private RemoteMainInfoInteractor mInteractor;
    
      public MainPresenter(Context context){
        this.mContext = context;
        mInteractor = new RemoteMainInfoInteractor (this);
      }
    
      /**
       * Activity中调用的方法示例
       */
      public void loadData(String paramet){
        mInteractor.loadData(paramet);
      }
      
    
       @Override
        public void onResult(String json){
          if(mView != null){
            mView.showToast(json);
          }
        }
      
       @Override
       public void attachView(MainPageView view){
        this.mView = view;
       }
    
      @Override
      public void detachView(){
         this.mView = null;
      }
    }
    

    M层接口实现

    关系流程:M-->P

    public class RemoteMainInfoInteractor implements MainInfoInteractor{
    
      private Callback mCallback;
      
      public RemoteMainInfoInteractor(Callback callback){
        this.mCallback = callback;
      }
    
      @Override
      public void loadData(String paramet, Callback callback){
        ...
        String json = ...;
        if(callback != null){
          callback.onResult(json);
        }
      }
    
      @Override
      public void loadData(String paramet){
        ...
        String json = ...;
        if(mCallback != null){
          mCallback.onResult(json);
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:Android MVP

          本文链接:https://www.haomeiwen.com/subject/thjeoftx.html