我希望您能告诉我如何正确实现具有导航抽屉的MapFragment。我已经成功地实现了这个功能,但我一直在阅读一些文档,发现可能会在将来出现问题。
因此,我的主类是一个非常普通的类,其中已经实现了导航抽屉,并具有以下selectItem()方法:
请注意,这是一个在RelativeLayout里的mapFragment,并且已经正常工作。但是,如果我删除RelativeLayout,只留下mapFragment,它会崩溃,因为我正在一个fragment中充气另一个fragment。所以,有以下问题:
因此,我的主类是一个非常普通的类,其中已经实现了导航抽屉,并具有以下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时不会崩溃?它们不是本质上相同的吗?
- 什么是正确的实现方法?