如何在其他Fragment中重用SupportMapFragment

时间:2015-02-23 20:08:35

标签: android-maps-v2 android-maps android-maps-extensions

我刚刚切换到最新版本的android-maps-extensions(2.2.0)以及最新的Play Services(6.5.87)和支持库(21.0.3)。 现在我无法重用我的MapFragment。

我有NavigationArawer的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;
    }

始终用内部重新创建视图。但是我有成千上万的标记(当然有很棒的聚类)并且需要很多时间来重新绘制它们。

我知道它不是扩展程序库的问题,Google地图具有相同的行为。但也许你知道正确的决定?

1 个答案:

答案 0 :(得分:3)

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

由于以编程方式添加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);
            }
        });
    }
}