如何在其他Fragment中重用SupportMapFragment



我刚刚切换到最新版本的android地图扩展(2.2.0)以及最新的Play Services(6.5.87)和支持库(21.0.3)。现在我不能再使用我的MapFragment了。

我有NavigationDrawer的MainActivity。其中一个片段是里面有GoogleMap片段的片段。当我在NavigationDrawer中的片段之间切换时,它们会重新创建自己。以前我对此使用了非常简单的解决方案。我使用了已经膨胀的视图:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }
    view = inflater.inflate(R.layout.fragment_map, container, false);
    SupportMapFragment mapFragment = SupportMapFragment.newInstance();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.container, mapFragment).commit();
    mapFragment.getExtendedMapAsync(new OnMapReadyCallback() {
        public void onMapReady(GoogleMap googleMap) {
            map = googleMap;
            setupMap();
        }
    });
    return view;
}

但现在它不起作用了。当这个片段第二次打开时,地图不会显示。

我可以扔掉这个丑陋的代码

 if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

并且总是用里面的东西重新创造视野。但我有成千上万的标记(当然还有很棒的聚类),重新绘制它们需要很多时间。

我知道这不是扩展库的问题,谷歌地图也有同样的行为。但也许你们知道正确的决定?

我们可以使用MapView,而不是使用MapFragment,它也存在于androidmapsextracts中。而且它可以重复使用。

由于是通过程序添加MapView的,因此需要调用MapView.onCreate(bundle)

private MapView mapView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_map, container, false);
    if (mapView != null) {        
        ViewGroup parentViewGroup = (ViewGroup) mapView.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeView(mapView);            
        }
    } else {
        mapView = new MapView(getActivity());
        mapView.onCreate(Bundle.EMPTY); //need if programmatically add 
    }    
    ((ViewGroup)view.findViewById(R.id.container)).addView(mapView);
    mapView.getExtendedMapAsync(new OnMapReadyCallback() {
        public void onMapReady(GoogleMap googleMap) {
            setUpMap(GoogleMap);
        }
    });    
}    

或者,如果我们不需要任何重复使用的mapView 设置

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_map, container, false);
    boolean needSetupMap = true;
    if (mapView != null) {        
        ViewGroup parentViewGroup = (ViewGroup) mapView.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeView(mapView);
            needSetupMap = false;
        }
    } else {
        mapView = new MapView(getActivity());
        mapView.onCreate(Bundle.EMPTY);
    }  
    ((ViewGroup)view.findViewById(R.id.container)).addView(mapView);
    if (needSetupMap) {
        mapView.getExtendedMapAsync(new OnMapReadyCallback() {
            public void onMapReady(GoogleMap googleMap) {
                setUpMap(GoogleMap);
            }
        });
    }
}