通过覆盖onCreateView()将SupportMapFragment放到另一个Fragment的方法

时间:2013-09-23 14:49:10

标签: android google-maps-android-api-2 android-support-library

我会试着描述一下我的情况:

MyFragment extends 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>
MyMapWidget.java

中的

public class MyMapWidget extends LinearLayout {

  public MyMapWidget(final Context context, AttributeSet attrs) {
        super(context, attrs);
       ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.widget_map, this, true);
...
}
widget_map.xml

中的

<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时 - 一切都很棒。但是,如果那时我显示另一个片段(带有清理后台),然后再显示MyFragment - 我得到了Inflate Exception

Stacktrase

android.view.InflateException: Binary XML file line #151: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at `com.***.****.fragments.MyFragment.onCreateView(MyFragment.java:78)`

MyFragment.java中的第78行(方法onCreateView

View view = inflater.inflate(R.layout.my_fragment, null);

MyMapWidget中移除my_fragment.xml解决了问题。但我有一些问题:

  1. 为什么会这样?
  2. 我可以这样显示地图吗?
  3. 您是否可以通过另一种方式了解当前地图如何成为自己Fragment的一部分?
  4. 注意:我检查了类似question的答案,但无法解决我的情况。

3 个答案:

答案 0 :(得分:2)

您可以将地图放在其自己的片段中,如下所示:

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

让您的活动实施 OnGoogleMapFragmentListener ,然后在添加地图并准备就绪时,将调用以下方法:

@Override
public void onMapReady(GoogleMap map) {
       //add markers or whatever
}

希望这对您更有效地控制mapfragment非常有用。

答案 1 :(得分:2)

我记得前一段时间遇到类似的问题。对我有用的是以编程方式在片段onCreateView方法内创建地图。像.-

之类的东西
@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();
    }
}

答案 2 :(得分:0)

我找到了方法,如何解决它:

  1. 检测MyFragment销毁其观点(onDestroyView
  2. 的事件
  3. 在此方法中获取MyMapWidget的实例并调用destroyMap
  4. myMapWidget.destroyMap将是:

    public void destroyMap(FragmentManager fm){         。fm.beginTransaction()除去(mMapFragment).commit();
    }

  5. 当父片段破坏他们的视图时,只需删除带有地图的片段。