本文共 3336 字,大约阅读时间需要 11 分钟。
public interface BaseView { void showLoading(); void hideLoading();}
public interface NewMvpView extends BaseView { void setListItem(Listdata); void showMessage(String message);}
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); }}
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();}
public class NewMvpActivity extends BaseMvpActivityimplements 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/