MVP基本样式
model层
IModel
public interface IModel { //订单数据 void getOrderData(String url, Map<String, String> map, OrderLoadListener orderLoadListener); }
ModelImpl
public class ModelImpl implements IModel { //获取订单数据 @Override public void getOrderData(String url, Map<String, String> map, final OrderLoadListener orderLoadListener) { OkHttpUtil instance = OkHttpUtil.getInstance(); instance.okPost(url, map); instance.setOkLoadListener(new OkLoadListener() { @Override public void setOkLoadSuccess(String json) { orderLoadListener.loadOrderSuccess(json); } @Override public void setOkLoadError(String error) { orderLoadListener.loadOrderError(error); } }); } }
OrderLoadListener
public interface OrderLoadListener { void loadOrderSuccess(String json); void loadOrderError(String error); }
presenter层
IPresenter
public interface IPresenter { //加载数据并展示到视图上 void showDataToOrder(Context context, IModel iModel, OrderView orderView); }
PresenterImpl
public class PresenterImpl implements IPresenter { private static final String TAG = "PresenterImpl"; @Override public void showDataToOrder(final Context context, IModel iModel, final OrderView orderView) { HashMap<String, String> map = new HashMap<>(); map.put("uid", "71"); iModel.getOrderData(HttpConfig.order_url, map, new OrderLoadListener() { @Override public void loadOrderSuccess(String json) { Gson gson = new Gson(); OrderBean orderBean = gson.fromJson(json, OrderBean.class); String code = orderBean.getCode(); Log.d(TAG, "loadOrderSuccess: ----" + orderBean.getData().get(0).getTitle()); //将数据传给View orderView.getDataToView(context, orderBean); } @Override public void loadOrderError(String error) { Log.d(TAG, "loadOrderError: ---" + error); } }); } }
util
public class HttpConfig { public static String order_url = "https://www.zhaoapi.cn/product/getOrders"; public static String cancel_order_url = "https://www.zhaoapi.cn/product/updateOrder"; }
OkLoadListener
public interface OkLoadListener { void setOkLoadSuccess(String json); void setOkLoadError(String error); }
View层
orderView
public interface OrderView { //获取数据展示到视图上 void getDataToView(Context context, OrderBean orderBean); }
Mainactivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener, OrderView { private FrameLayout order_fl; private ImageView order_menu; private View view; private RadioButton pop_daizhifu; private RadioButton pop_yizhifu; private RadioButton pop_yiquxiao; private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = View.inflate(this, R.layout.menu_window, null); initView(); PresenterImpl presenter = new PresenterImpl(); presenter.showDataToOrder(MainActivity.this, new ModelImpl(), this); } private void initView() { order_fl = findViewById(R.id.order_fl); pop_daizhifu = view.findViewById(R.id.pop_daizhifu); pop_yizhifu = view.findViewById(R.id.pop_yizhifu); pop_yiquxiao = view.findViewById(R.id.pop_yiquxiao); order_menu = findViewById(R.id.order_menu); order_menu.setOnClickListener(this); pop_daizhifu.setOnClickListener(this); pop_yizhifu.setOnClickListener(this); pop_yiquxiao.setOnClickListener(this); order_menu.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.order_menu://实现点击取消按钮弹出Dialog窗口(5分) popupWindow = new PopupWindow(view); popupWindow.setWidth(150); popupWindow.setHeight(200); popupWindow.showAsDropDown(order_menu); break; case R.id.pop_daizhifu: popupWindow.dismiss(); break; case R.id.pop_yizhifu: popupWindow.dismiss(); break; case R.id.pop_yiquxiao: popupWindow.dismiss(); break; } } private static final String TAG = "MainActivity"; @Override public void getDataToView(Context context, OrderBean orderBean) { //获取数据并展示 Log.d(TAG, "getDataToView: -----" + orderBean.getData().get(0).getTitle()); } }