我想知道使用导航抽屉实现MapFragment的正确方法是什么。我已经有这个工作正常,但我一直在阅读一些文件,也许它可能会在未来造成麻烦。导航抽屉和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;
}
}
而且,fragment_map.xml
其在MapaFragment
膨胀是这样的:
<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,它就会崩溃,因为我正在给一个片段内的一个片段充气。因此,存在的问题:
- 当它膨胀的mapFragment但里面的mapFragment充气的RelativeLayout时,它不会崩溃为什么它会崩溃?它不是基本相同吗?
- 什么是正确的方法来实现类似的东西?
你有没有发现?任何方法? – levi
没有什么,对不起:( – moictab