2016-11-26 22 views
0

我有一個應用程序,其中有一個主要活動和幾個片段,導航到導航抽屜。問題與多個地圖片段重疊

我在我的應用程序中使用了兩個地圖片段,這似乎會導致問題,如果我從片段地圖A,然後回到片段地圖B.在frgamnet中,我失去了對它們的控制權地圖通常只是顯示了最後一個fragmnet地圖的快照。

這似乎是一個已知的問題見。

google maps api bug report with issue

我作爲sugested上abouve4米解決方案是加載譜圖B這會導致問題沒有發生,因爲它不會佔用屏幕空間之前minimive圖A。

public void hideStupidMaps() { 
    mMapView.getLayoutParams().height = 1; 
    mMapView.getLayoutParams().width = 1; 
    mMapView.invalidate(); 
    mMapView.requestLayout(); 

} 

上面的方法在我的Gmap片段類中。我想從myMain Activity類調用它。在navagation抽屜代碼中。我的問題是我如何從主要活動中調用frgments方法。特別是在改變片段視圖時。我需要誇大的觀點和它的上述方法?

回答

0

您可以將多個fragments組合到單個活動中,以構建多窗格用戶界面並在多個活動中重用片段。片段必須始終嵌入到活動中,片段的生命週期直接受到宿主活動生命週期的影響。

要從onCreateView()返回佈局,可以從XML中定義的佈局資源中擴充它。爲了幫助您做到這一點,onCreateView()提供了一個LayoutInflater對象。

public static class ExampleFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.example_fragment, container, false); 
    } 
} 

下面是你可以替換另一個片段,並在後面的堆棧保留以前的狀態:

// Create new fragment and transaction 
Fragment newFragment = new ExampleFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit();