【问题标题】:Navigation Drawer and MapFragment导航抽屉和 MapFragment
【发布时间】:2014-04-28 14:36:56
【问题描述】:

我想知道用导航抽屉实现 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,即主Activity中的膨胀片段,如下:

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.xmlMapaFragment 中膨胀是这样的:

<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>

请注意,这是一个相对布局内的 mapFragment,它已经运行良好。但是,如果我删除了RelativeLayout 并且只留下了mapFragment,它会崩溃,因为我在片段内膨胀了一个片段。那么问题来了:

  • 为什么当它膨胀 mapFragment 时它会崩溃,但当它膨胀带有 mapFragment 的 RelativeLayout 时它不会崩溃?是不是本质上是一样的?
  • 实现这样的事情的正确方法是什么?

【问题讨论】:

  • 你找到什么了吗?有什么办法吗?
  • 什么都没有,对不起:(

标签: android google-maps android-layout android-fragments


【解决方案1】:

MapFragment 有一个非常相似的问题。这是我的解决方案:

我扩展 SupportMapFragment 对象:

public class MyMapFragment extends SupportMapFramgment
{

}

并以编程方式添加:

MyMapFragment fragment = new MyMapFragment();
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();

【讨论】:

  • 这里的 content_frame 是什么?
  • 这是放置地图片段的布局 ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2015-01-28
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多