Android的MVP

关于AndroidMVP模式

 

M:Model

V:view

P:Presenter

 

M:用来处理比较繁杂的业务逻辑

V:负责界面的更新和界面数据的获取,也可以和之前一般的做法,处理一些比较简易的逻辑

P:介于MV之间,个人理解的作为MV的连接通信的桥梁

 

View一般的做法是定义一个接口,用于处理更新等业务

比如下面的接口:

public interface IPlaceActivityViewForModel {

    void setTextForTextView(String text);
    void getLocationResult(LocationResultBean bean);
    void getReverseGeoCodeResultBean(ReverseGeoCodeResult beanResult, LatLng locationLatLng);
    void showToastText(String text);
}

 

在对应的Activity实现上述接口的方法

比如(这是一个百度地图的例子):


public class ChoosePlaceOnMapActivity extends Activity implements IPlaceActivityViewForModel {

    private MapView mMapView = null;
    private BaiduMap mBaiduMap = null;
    private LocationClient mLocationClient = null;
    private GeoCoder mGeoCoder = null;// 搜索,地理编码反编码
    private Double latitude;
    private Double longitude;
    private TextView text_view_show_id;

    private LatLng geocoderLatLng = null;
    private PlacePointPresenter placePointPresenter = new PlacePointPresenter(this);


    @Override
    protected void onStart() {
        super.onStart();
        getLocation();
        getGeoCoder();

        placePointPresenter.setIPlaceActivityViewForModel(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_onmap);
        mMapView = (MapView) this.findViewById(R.id.place_map);
        mBaiduMap = mMapView.getMap();
        text_view_show_id = (TextView) this.findViewById(R.id.text_view_show_id);
    }


    private void getMap(){

        //获取屏幕高度宽度
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

    //    mMapView.removeViewAt(1);        //去掉logo
        mMapView.showZoomControls(true);


        //获得百度地图状态
        MapStatus.Builder builder = new MapStatus.Builder();
//      builder.targetScreen(new Point(width/2,height/2));
        //定位的坐标点
        LatLng latLng = new LatLng(latitude, longitude);
//      BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher);
//      OverlayOptions overlay = new MarkerOptions().position(latLng).icon(bitmap);
//      map.addOverlay(overlay);
        //设置地图中心为定位点
        builder.target(latLng);
        //设置缩放级别 18对应比例尺50
        builder.zoom(18);
        MapStatus mapStatus = builder.build();
        MapStatusUpdate m = MapStatusUpdateFactory.newMapStatus(mapStatus);
        mBaiduMap.setMapStatus(m);
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {

            @Override
            public void onMapStatusChangeStart(MapStatus arg0) {
                // TODO Auto-generated method stub

            
}

            @Override
            public void onMapStatusChangeFinish(MapStatus arg0) {
                // TODO Auto-generated method stub
                
LatLng target = mBaiduMap.getMapStatus().target;
                System.out.println(target.toString());
                mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(target));
            }

            @Override
            public void onMapStatusChange(MapStatus arg0) {
                // TODO Auto-generated method stub

            
}
        });
    }

    private void getLocation(){
        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient
        placePointPresenter.setInitBdLocation(mLocationClient);
    }

    public void showToastText(String text){
        Toast.makeText(ChoosePlaceOnMapActivity.this, text, Toast.LENGTH_LONG).show();
    }

    private void getGeoCoder(){
        mGeoCoder = GeoCoder.newInstance();
        placePointPresenter.setInitGeoCoder(mGeoCoder);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        
super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        
super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        
super.onDestroy();
        mMapView.onDestroy();
        mLocationClient.stop();
        mGeoCoder.destroy();
    }

    @Override
    public void setTextForTextView(String text) {
        text_view_show_id.setText(text);
    }

    @Override
    public void getLocationResult(LocationResultBean bean) {
        latitude = bean.latitude;
        longitude = bean.longitude;
        setTextForTextView(bean.address);
        getMap();
    }

    @Override
    public void getReverseGeoCodeResultBean(ReverseGeoCodeResult beanResult, LatLng locationLatLng) {
        geocoderLatLng = beanResult.getLocation();
    }

    /**
     * 确定按钮的响应
     *
@param view
     */
    
public void onClickForButtonYes(View view){
        String placeText = text_view_show_id.getText().toString();
        Intent intent = new Intent();
        intent.putExtra("LatLng", geocoderLatLng);
        intent.putExtra("text", placeText);
        ChoosePlaceOnMapActivity.this.setResult(3, intent);
        ChoosePlaceOnMapActivity.this.finish();
    }

}

 

其中PlacePointPresenter

就是Presenter,具体代码如下,当中使用到上面定义的接口,接口中的方法在Activity,也就是我们说的View层中实现,在Presenter中进行调用,那么调用的地方一般就是在业务逻辑处理后,具体根据需要做判断吧,当中自然也就包含了业务逻辑实现的类DealWithPlacePointForModelImpl ,此类内容在本文中就不再给出了。


public class PlacePointPresenter {

    private static final String TAG = "PlacePointPresenter";

    private IPlaceActivityViewForModel iPlaceActivityViewForModel;
    private Context context;

    private DealWithPlacePointForModelImpl dealWithPlacePointForModel;


    public PlacePointPresenter(Context context){
        this.context = context;
        dealWithPlacePointForModel = new DealWithPlacePointForModelImpl(context);
    }

    public void setIPlaceActivityViewForModel(IPlaceActivityViewForModel iPlaceActivityViewForModel){
        this.iPlaceActivityViewForModel = iPlaceActivityViewForModel;
    }

    public void setInitGeoCoder(GeoCoder mGeoCoder){
        dealWithPlacePointForModel.doGeoCoder(mGeoCoder, new DealWithPlacePointForModelImpl.OnGeoCoderResultListener() {
            @Override
            public void onReverseGeoCode(ReverseGeoCodeResult result) {
                iPlaceActivityViewForModel.setTextForTextView(result.getAddress());
                iPlaceActivityViewForModel.getReverseGeoCodeResultBean(result, result.getLocation());
            }

            @Override
            public void onGeoCoder(GeoCodeResult result) {
            }
            @Override
            public void onNoResult(String text) {
               iPlaceActivityViewForModel.showToastText(text);
            }
        });
    }


    public void setInitBdLocation(LocationClient mLocationClient){
       dealWithPlacePointForModel.doLocation(mLocationClient, new DealWithPlacePointForModelImpl.OnLocationStatusListener() {
           @Override
           public void onLocationStatusSuccess(LocationResultBean bean) {
               iPlaceActivityViewForModel.getLocationResult(bean);
           }
       });
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值