间重用单活动几个片段谷歌地图V2 [英] Reuse Google Map v2 across several fragments in single Activity

查看:160
本文介绍了间重用单活动几个片段谷歌地图V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个活动开关,它包含谷歌地图的碎片。

起初我试图用碎片的孩子片段mananger(ChildFragmentManager)中加入 SupportMapFragment 。它的工作原理确定,但地图被再次加载的每个片段,这需要大量的时间。

我已经无法从一个片段分离 SupportMapFragment ,并将其连接到另一个,因为它在某种程度上与的ViewGroup 连接它被放在第一。

所以最后我决定用纯图形页面对象,它是创建活动,并在另一个片段的ViewGroup放置前将其添加到的ViewGroup活性片段,并删除它。

一切工作正常,除了的onSaveInstanceState (图形页面需要手动通过调用所有活动的生命周期方法,如 onResume 的onPause 的onDestroy 的onSaveInstanceState )。当我尝试调用 mapView.onSaveInstanceState 在我的活动压伤的应用程序。

我不知道其他人在几个片段已经成功地重新使用GoogleMap的?


解决方案

我使用的地图片段列表项里面 - 我有一个被创建并添加到查看只是一个片段一次且仅一次 - 我有一个 ExpandableListView ,我在将它添加适配器 getChildView

One of my activities switches the fragments that contains google map.

At first I tried to use fragment's child fragment mananger (ChildFragmentManager) to add SupportMapFragment. It works ok, but map is loaded again for each fragment, which takes a lot of time.

I have failed to detach SupportMapFragment from one fragment and attach it to another, since it somehow connected with ViewGroup where it was placed first.

So finally I decided to use plain MapView object that is create in activity and add it to viewgroup of active fragment and remove it before placing in viewgroup of another fragment.

Everything works ok except onSaveInstanceState (MapView requires manually pass call all activity lifecycle methods like onResume, onPause, onDestroy, onSaveInstanceState). When I try to call mapView.onSaveInstanceState in my activity it crushes the app.

I wonder if anybody else have successfully reused GoogleMap across several fragments?

解决方案

I'm using map fragment inside list item - I have just one fragment which is created and added to the View once and only once - I have an ExpandableListView and I'm adding it in adapter's getChildView.

这篇关于间重用单活动几个片段谷歌地图V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆