所以我有一个应用程序在两个不同的活动中使用MapFragent
。第一个是在该活动的xml布局中实现的,但是很多人可能都知道,在不同的活动中实现另一个地图片段的唯一方法就是在java中这样做:
mMapFragment = new DRPCustomMapFragment();// this is just a class extending SupportMapFragment
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.mapContainer, mMapFragment);
fragmentTransaction.commit();
fm.executePendingTransactions();
//
所以在onCreate()
我正是这样做的。问题是如果我在此之后立即尝试引用mMapFragment,它仍然为null。所以我最终不得不添加这个丑陋的代码块:
mMapFragment = new DRPCustomMapFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.mapContainer, mMapFragment);
fragmentTransaction.commit();
fm.executePendingTransactions();
//
_mapContainer.post(new Runnable() {
@Override
public void run() {
_map = mMapFragment.getMap();
mMapFragment
.setOnDragListener(new DRPMapWrapperLayout.OnDragListener() {
@Override
public void onDrag(MotionEvent motionEvent) {
Log.d("ON_DRAG",
String.format("ME: %s",
motionEvent.getAction()));
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
_scrollView
.requestDisallowInterceptTouchEvent(true);
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP
|| motionEvent.getAction() == MotionEvent.ACTION_OUTSIDE) {
_scrollView.requestDisallowInterceptTouchEvent(false);
_locationForDrop = _map.getCameraPosition().target; }
}
});
setUpMapUI();
}
});
请告诉我有更好的方法..很高兴吗?