【问题标题】:Issue with multiple map fragments overlapping多个地图片段重叠的问题
【发布时间】:2016-11-26 12:05:25
【问题描述】:

我有一个应用程序,它有一个主要活动和几个通过抽屉导航导航到的片段。

我在我的应用程序中使用了两个地图片段,如果我从片段地图 A 然后回到片段地图 B ,这似乎会导致问题。在 fragmnet b 中,我无法控制它们,map 通常只显示 fragmnet map A 最后所在位置的快照。

这似乎是一个已知问题,请参阅。

google maps api bug report with issue

我建议的解决方案是在加载地图 B 之前最小化地图 A,这会导致问题不会发生,因为它不会占用屏幕空间。

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

}

上述方法在我的 Gmap 片段类中。我想从 myMain Activity 类中调用它。在导航抽屉代码中。我的问题是如何从主要活动中调用 frgments 方法。特别是在更改该片段视图时。我需要将视图膨胀到上述方法吗?

【问题讨论】:

    标签: java android google-maps android-fragments


    【解决方案1】:

    您可以在单个活动中组合多个fragments 以构建多窗格 UI 并在多个活动中重复使用片段。片段必须始终嵌入到活动中,并且片段的生命周期直接受宿主活动生命周期的影响。

    要从 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();
    

    【讨论】:

      猜你喜欢
      • 2013-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-30
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多