[英]How to reuse SupportMapFragment inside other Fragment
I just switched to the newest version of android-maps-extensions (2.2.0) together with newest Play Services (6.5.87) and support library (21.0.3). 我刚刚切换到最新版本的android-maps-extensions(2.2.0)以及最新的Play Services(6.5.87)和支持库(21.0.3)。 And now I can't reuse my MapFragment.
现在我无法重用我的MapFragment。
I have MainActivity with NavigationDrawer. 我有NavigationArawer的MainActivity。 One of the fragments is fragment with GoogleMap fragment inside.
其中一个片段是里面有GoogleMap片段的片段。 When I switch between fragments in NavigationDrawer they recreate themselves.
当我在NavigationDrawer中切换片段时,他们重新创建自己。 Previously I used very simply solution for this.
以前我使用非常简单的解决方案。 I used already inflated view:
我用过已经夸大的视图:
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;
}
But now it doesn't work. 但现在它不起作用。 Map doesn't show when this fragment opens second time.
此片段第二次打开时,地图不会显示。
I can just throw out this ugly code 我可以抛弃这个丑陋的代码
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null) {
parent.removeView(view);
}
return view;
}
And always recreate view with inside. 并始终用内部重新创建视图。 But I have thousands of Markers (of course with awesome Clustering) and it takes many time to repaint them.
但是我有成千上万的标记(当然有很棒的聚类)并且需要很多时间来重新绘制它们。
I know it's not the problem of extensions-library, Google Maps have the same behavior. 我知道这不是扩展库的问题,谷歌地图有相同的行为。 But maybe you know right decision?
但也许你知道正确的决定?
Instead of using MapFragment we can use MapView, that exists in androidmapsextensions too. 我们可以使用MapView,而不是使用MapFragment,它也存在于androidmapsextensions中。 And it can be reused.
它可以重复使用。
Since adding MapView programatically, need to call mapView.onCreate(bundle) 由于以编程方式添加MapView,需要调用mapView.onCreate(bundle)
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);
}
});
}
Or if we don't need any setup for reused 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);
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);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.