Android 通过重写onCreateView()将SupportMapFragment放入另一个片段的方法
我将尝试描述我的情况: 我有Android 通过重写onCreateView()将SupportMapFragment放入另一个片段的方法,android,google-maps-android-api-2,android-support-library,Android,Google Maps Android Api 2,Android Support Library,我将尝试描述我的情况: 我有MyFragment扩展了Fragment,覆盖了onCreateView() 在MyFragment.java中 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflat
MyFragment扩展了Fragment
,覆盖了onCreateView()
在MyFragment.java中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, null);
//some manipulation with view
return view;
在my_fragment.xml
<LinearLayout
.....
>
<com.example.widgets.MyMapWidget
android:id="@+id/my_map"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Some another views-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
<!-- Some another views-->
</LinearLayout>
在widget_map.xml中
<LinearLayout
.....
>
<com.example.widgets.MyMapWidget
android:id="@+id/my_map"
android:layout_width="match_parent"
android:layout_height="300dp"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Some another views-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
/>
<!-- Some another views-->
</LinearLayout>
MyFragment.java
中的第78行(方法onCreateView
)
从myu fragment.xml
中删除MyMapWidget
解决了问题。但我有一些问题:
为什么会这样
我可以这样展示地图吗
也许你知道另一种方式如何呈现自己的片段的一部分
注意:我检查了类似问题的答案,但无法解决我的问题。您可以将地图放在它自己的片段中,如下所示:
public class GoogleMapFragment extends MapFragment {
private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";
public GoogleMapFragment() {
mCallback = null;
}
public static interface OnGoogleMapFragmentListener {
void onMapReady(GoogleMap map);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public static GoogleMapFragment newInstance() {
return new GoogleMapFragment();
}
public static GoogleMapFragment newInstance(GoogleMapOptions options) {
Bundle arguments = new Bundle();
arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);
GoogleMapFragment fragment = new GoogleMapFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnGoogleMapFragmentListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().getClass().getName()
+ " must implement OnGoogleMapFragmentListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (mCallback != null) {
mCallback.onMapReady(getMap());
}
return view;
}
private OnGoogleMapFragmentListener mCallback;
}
// create a new map
mapsFragment = GoogleMapFragment.newInstance();
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, mapsFragment, FRAGMENT_MAP_TAG);
fragmentTransaction.commit();
并将其添加到您的活动中,如下所示:
public class GoogleMapFragment extends MapFragment {
private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";
public GoogleMapFragment() {
mCallback = null;
}
public static interface OnGoogleMapFragmentListener {
void onMapReady(GoogleMap map);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public static GoogleMapFragment newInstance() {
return new GoogleMapFragment();
}
public static GoogleMapFragment newInstance(GoogleMapOptions options) {
Bundle arguments = new Bundle();
arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);
GoogleMapFragment fragment = new GoogleMapFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnGoogleMapFragmentListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().getClass().getName()
+ " must implement OnGoogleMapFragmentListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (mCallback != null) {
mCallback.onMapReady(getMap());
}
return view;
}
private OnGoogleMapFragmentListener mCallback;
}
// create a new map
mapsFragment = GoogleMapFragment.newInstance();
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, mapsFragment, FRAGMENT_MAP_TAG);
fragmentTransaction.commit();
让您的活动实现OnGoogleMapFragmentListenerOnGoogleMapFragmentListener,然后在添加映射并准备就绪后,将调用以下方法:
@Override
public void onMapReady(GoogleMap map) {
//add markers or whatever
}
希望这有助于您更好地控制mapfragment。您可以将地图放在自己的片段中,如下所示:
public class GoogleMapFragment extends MapFragment {
private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";
public GoogleMapFragment() {
mCallback = null;
}
public static interface OnGoogleMapFragmentListener {
void onMapReady(GoogleMap map);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public static GoogleMapFragment newInstance() {
return new GoogleMapFragment();
}
public static GoogleMapFragment newInstance(GoogleMapOptions options) {
Bundle arguments = new Bundle();
arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);
GoogleMapFragment fragment = new GoogleMapFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnGoogleMapFragmentListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().getClass().getName()
+ " must implement OnGoogleMapFragmentListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (mCallback != null) {
mCallback.onMapReady(getMap());
}
return view;
}
private OnGoogleMapFragmentListener mCallback;
}
// create a new map
mapsFragment = GoogleMapFragment.newInstance();
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, mapsFragment, FRAGMENT_MAP_TAG);
fragmentTransaction.commit();
并将其添加到您的活动中,如下所示:
public class GoogleMapFragment extends MapFragment {
private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";
public GoogleMapFragment() {
mCallback = null;
}
public static interface OnGoogleMapFragmentListener {
void onMapReady(GoogleMap map);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public static GoogleMapFragment newInstance() {
return new GoogleMapFragment();
}
public static GoogleMapFragment newInstance(GoogleMapOptions options) {
Bundle arguments = new Bundle();
arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);
GoogleMapFragment fragment = new GoogleMapFragment();
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnGoogleMapFragmentListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().getClass().getName()
+ " must implement OnGoogleMapFragmentListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (mCallback != null) {
mCallback.onMapReady(getMap());
}
return view;
}
private OnGoogleMapFragmentListener mCallback;
}
// create a new map
mapsFragment = GoogleMapFragment.newInstance();
// Then we add it using a FragmentTransaction.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(android.R.id.content, mapsFragment, FRAGMENT_MAP_TAG);
fragmentTransaction.commit();
让您的活动实现OnGoogleMapFragmentListenerOnGoogleMapFragmentListener,然后在添加映射并准备就绪后,将调用以下方法:
@Override
public void onMapReady(GoogleMap map) {
//add markers or whatever
}
希望这有助于您更好地控制mapfragment。我记得不久前遇到过类似的问题。对我来说,有效的方法是在fragmentonCreateView
方法中以编程方式创建映射。差不多-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_layout);
setupMap();
return view;
}
private void setupMap() {
mapFragment = new SupportMapFragment();
ParentActivity activity = getParentActivity();
if (activity != null) {
FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.wrapperMapPlaces, mapFragment);
fragmentTransaction.commit();
}
}
我记得不久前我也遇到过类似的问题。对我有效的是通过编程方式在fragmentonCreateView
方法中创建映射。差不多-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_layout);
setupMap();
return view;
}
private void setupMap() {
mapFragment = new SupportMapFragment();
ParentActivity activity = getParentActivity();
if (activity != null) {
FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.wrapperMapPlaces, mapFragment);
fragmentTransaction.commit();
}
}
我找到了方法,如何解决它:
检测MyFragment
破坏其视图的事件(onDestroyView
)
在这个方法中,获取MyMapWidget
的实例并调用destromap
myMapWidget.destroyMap
将是:
公共地图(碎片管理器fm){
fm.beginTransaction().remove(mMapFragment.commit();
}
当父片段解压视图时,只需使用map删除片段即可。我找到了方法,如何解决它:
检测MyFragment
破坏其视图的事件(onDestroyView
)
在这个方法中,获取MyMapWidget
的实例并调用destromap
myMapWidget.destroyMap
将是:
公共地图(碎片管理器fm){
fm.beginTransaction().remove(mMapFragment.commit();
}
当父片段解压视图时,只需使用map移除片段