2015-02-23 41 views
4

我剛剛切換到最新版本的android-maps-extensions(2.2.0)以及最新的Play Services(6.5.87)和支持庫(21.0.3)。 現在我不能重用我的MapFragment。如何在其他片段內重新使用SupportMapFragment

我有MainActivity與NavigationDrawer。其中一個片段是帶有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; 
    } 

並始終重新視圖裏面。但是我有成千上萬的標記(當然還有令人敬畏的集羣),並且需要很多時間來重繪它們。

我知道這不是擴展庫的問題,谷歌地圖有相同的行爲。但是,也許你知道正確的決定?

+0

同一問題的重複使用MapView的任何設置。似乎是玩圖書館的錯誤。 – zyamys 2015-03-06 07:25:38

回答

3

除了使用MapFragment,我們還可以使用MapView,它也存在於androidmapsextensions中。它可以被重用。

由於編程加入MapView類,需要調用mapView.onCreate(束)

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); 
     } 
    });  
}  

或者,如果我們不需要在這裏

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); 
      } 
     }); 
    } 
} 
相關問題