美文网首页
mvp架构优化

mvp架构优化

作者: 第四风111 | 来源:发表于2018-08-20 18:53 被阅读235次

    MVP优化的结构

    mvp结构图.png

    view的结构

    • BaseView
    public interface BaseView {
    
        void showLoading();
        void hideLoading();
    
    }
    
    • NewMvpView
    public interface NewMvpView extends BaseView {
        void setListItem(List<String> data);
        void showMessage(String message);
    }
    

    presenter结构

    • BasePresenter
    public abstract class BasePresenter<T> {
        public T mView;
    
        public void attach(T mView){
            this.mView = mView;
        }
    
        public void dettach(){
            mView = null;
        }
    }
    

    NewMvpPresenter

    public class NewMvpPresenter extends BasePresenter<NewMvpView> {
    
        private RequestBiz requestBiz;
        private Handler mHandler;
    
        public NewMvpPresenter() {
            requestBiz = new RequestBiziml();
            mHandler = new Handler(Looper.getMainLooper());
        }
    
        public void onResume(){
            requestBiz.requestForData(new OnRequestListener() {
    
                @Override
                public void onSuccess(final List<String> data) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mView.hideLoading();
                            mView.setListItem(data);
                        }
                    });
                }
    
                @Override
                public void onFailed() {
                    mView.showMessage("请求失败");
                }
            });
        }
    
        public void onItemClick(int position){
            mView.showMessage("点击了item"+position);
        }
    }
    

    activity结构

    • BaseMvpActivity
    public abstract class BaseMvpActivity<V,T extends BasePresenter<V>> extends AppCompatActivity {
    
        public T presenter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            presenter = initPresenter();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            presenter.attach((V)this);
        }
    
        @Override
        protected void onDestroy() {
            presenter.dettach();
            super.onDestroy();
        }
    
        public abstract T initPresenter();
    
    }
    
    • NewMvpActivity
    public class NewMvpActivity extends BaseMvpActivity<NewMvpView,NewMvpPresenter> implements NewMvpView,AdapterView.OnItemClickListener{
    
        private ListView mvpListView;
        private ProgressBar pb;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mvp);
            mvpListView = (ListView)findViewById(R.id.mvp_listview);
            mvpListView.setOnItemClickListener(this);
            pb = (ProgressBar) findViewById(R.id.mvp_loading);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            presenter.onResume();
        }
    
        @Override
        public NewMvpPresenter initPresenter() {
            return new NewMvpPresenter();
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            presenter.onItemClick(position);
        }
    
        @Override
        public void setListItem(List<String> data) {
            ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
            mvpListView.setAdapter(adapter);
        }
    
        @Override
        public void showMessage(String message) {
            Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void showLoading() {
            pb.setVisibility(View.VISIBLE);
        }
    
        @Override
        public void hideLoading() {
            pb.setVisibility(View.GONE);
        }
    }
    

    相关文章

      网友评论

          本文标题:mvp架构优化

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