2016-11-26 36 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();