导航抽屉和地图片段

5
我希望您能告诉我如何正确实现具有导航抽屉的MapFragment。我已经成功地实现了这个功能,但我一直在阅读一些文档,发现可能会在将来出现问题。
因此,我的主类是一个非常普通的类,其中已经实现了导航抽屉,并具有以下selectItem()方法:
HomeActivity extends Activity{

//...
//Stuff like onCreate(), etc.
//...

private void selectItem(int position) {
    if (position == 0) {  //Position 0 is the map option in the navigationDrawer
        Fragment fragment = new MapaFragment();
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment).commit();
    }
    mDrawerList.setItemChecked(position, true);
    setTitle(listaElementosDrawer[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
    }
}

MapaFragment是主活动中充气的片段,具体如下:

public class MapaFragment extends Fragment {

public MapaFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_map, container,
            false);
    return rootView;
}
}

另外,MapaFragment 中充气的 fragment_map.xml 如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.moictab.decanias.MapActivity$PlaceholderFragment" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

请注意,这是一个在RelativeLayout里的mapFragment,并且已经正常工作。但是,如果我删除RelativeLayout,只留下mapFragment,它会崩溃,因为我正在一个fragment中充气另一个fragment。所以,有以下问题:
  • 当填充mapFragment时为什么会崩溃,而仅填充包含mapFragment的RelativeLayout时不会崩溃?它们不是本质上相同的吗?
  • 什么是正确的实现方法?

你找到了什么吗?有什么方法吗? - levi
抱歉,没有任何内容需要翻译。 - moictab
1个回答

1
我是一个有用的助手,可以翻译文本。
我有一个与MapFragment非常相似的问题。这是我的解决方案:
我扩展了SupportMapFragment对象:
public class MyMapFragment extends SupportMapFramgment
{

}

并通过编程添加它:
MyMapFragment fragment = new MyMapFragment();
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();

这里的 content_frame 是什么? - rakesh kashyap
这是布局ID,用于放置您的地图片段。 - Matt Twig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接