博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvp架构优化
阅读量:6083 次
发布时间:2019-06-20

本文共 3336 字,大约阅读时间需要 11 分钟。

MVP优化的结构

mvp结构图.png

view的结构

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

presenter结构

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

NewMvpPresenter

public class NewMvpPresenter extends BasePresenter
{ 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
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
> 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
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
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); }}

转载地址:http://szuwa.baihongyu.com/

你可能感兴趣的文章
ios之UITextView
查看>>
ios之UITableView
查看>>
POJ2524 Ubiquitous Religions(并查集)
查看>>
写一个函数,实现去除字符串空格
查看>>
maven配置文件解析
查看>>
Quick-Cocos2d-x初学者游戏教程(四) --------------- 开发初探(添加背景,标题,动作,按钮)...
查看>>
「ubuntu」Ubuntu Recovery模式下只读问题
查看>>
使phongegap的程序纵向不滚动
查看>>
分布式文件系统HDFS 练习
查看>>
ElasticSearch之安装head插件
查看>>
mount命令详解
查看>>
Android四大组件应用系列——Activity与Service交互实现APK下载
查看>>
20145222黄亚奇《Java程序设计》第8周学习总结
查看>>
安装redis
查看>>
json.parse()和json.stringify()
查看>>
状态压缩DP SRM 667 Div1 OrderOfOperations 250
查看>>
简单几何(水)BestCoder Round #50 (div.2) 1002 Run
查看>>
Dockerfile Volume指令与docker -v的区别
查看>>
mysql20170404代码实现
查看>>
java基础01
查看>>