1.介绍
M->model:数据
V->view:activity
P->Presenter:连接M和V
2.基本使用
2.1 View层
(1)父类BaseView(IBaseView )
public interface IBaseView {
// 返回错误信息
void showErrorMsg(String errormsg);
}
(2)子类BaseView(BaseView)
public interface BaseView<T> extends IBaseView{
// 显示图片,文字
void showView(List<T> goodsDataList);
// 加载进度条
// 加载动画
}
2.2 model层
(1) 父类(IBaseModel)
public interface IBaseModel { void loadData(OnLoadData onLoadData); interface OnLoadData <T>{ void onComplete(List<T> goodsDataList); void onError(String msg); } }
(2)数据Model(GoodsModel)
public class GoodsModel implements IBaseModel { @Override public void loadData(OnLoadData onLoadData) { onLoadData.onComplete(getData()); } private List<GoodsData> getData() { ArrayList goodsList = new ArrayList(); for (int i = 0; i < 100; i++) { GoodsData goodsData = new GoodsData("", "内容1" + i, "内容2" + i); goodsList.add(goodsData); } return goodsList; } }
2.3 Presenter层
(1)父类Presenter(BasePresenter)
public class BasePresenter<T extends IBaseView> { WeakReference<T> iGoodView; // 绑定 public void attachView(T view){ iGoodView=new WeakReference<>(view); } // 解绑 public void detachView(){ if (iGoodView!=null){ iGoodView.clear(); iGoodView=null; } } }
(2)连接BaseView和GoodsModel的GoodsPresenter
public class GoodsPresenter<T extends IBaseView> extends BasePresenter { // 2.持有model接口 GoodsModel iGoodsModel = new GoodsModel(); // 3.持有业务逻辑 public void fetch() { if (iGoodView != null && iGoodsModel != null) { iGoodsModel.loadData(new IBaseModel.OnLoadData() { @Override public void onComplete(List goodsDataList) { ((BaseView) iGoodView.get()).showView(goodsDataList); } @Override public void onError(String msg) { } }); } } }
3.基础Activity(BaseActivity)
public abstract class BaseActivity<T extends BasePresenter,V extends IBaseView> extends AppCompatActivity { protected T presenter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 布局文件 setContentView(setLayout()); // 定义一个选择表示层 presenter=createPresenter(); presenter.attachView((V)this); // 初始化view initView(); // 注册 registerSDK(); // 销毁注册 unRegisterSDK(); } private void registerSDK() { } private void unRegisterSDK() { } protected abstract int setLayout(); protected abstract void initView(); protected abstract T createPresenter(); @Override protected void onDestroy() { super.onDestroy(); presenter.detachView(); } }
4.MainActivity
public class MainActivity extends BaseActivity<GoodsPresenter, IBaseView> implements BaseView { private ListView lvGoods; //private GoodsPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected int setLayout() { return R.layout.activity_main; } @Override protected void initView() { lvGoods=findViewById(R.id.lvGoods); presenter.fetch(); lvGoods.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); } @Override protected GoodsPresenter createPresenter() { return new GoodsPresenter(); } @Override public void showView(List goodsDataList) { lvGoods.setAdapter(new GoodsAdapter(MainActivity.this,goodsDataList)); } @Override public void showErrorMsg(String errormsg) { } }
5.其他界面可以重新写GoodsModel和GoodsPresenter