0

我有一个MainFragment是由两个片段,一个MapFragmentListFragment的,这是它的布局:获取地图视图实例:

MainFragment布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/cups_black" > 

<fragment 
    android:id="@+id/map_fragment" 
    android:name="com.citylifeapps.cups.fragments.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/black_layout" 
    android:layout_marginTop="160dp"   
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/cups_black" 
    android:visibility="gone" > 
</RelativeLayout> 

<fragment 
    android:id="@+id/list_fragment" 
    android:name="com.citylifeapps.cups.fragments.ListFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<Button 
    android:id="@+id/button_close_full_map" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_margin="10dp" 
    android:background="@drawable/map_close_button_selector" 
    android:text="@string/close" 
     android:visibility="gone" 
    android:textColor="@color/cups_black" /> 

<Button 
    android:id="@+id/button_show_path" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_margin="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:background="@drawable/map_close_button_selector" 
    android:text="@string/show_path" 
    android:visibility="gone" 
    android:textColor="@color/cups_black" /> 

    <Button 
     android:id="@+id/button_navigate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button_show_path" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/map_close_button_selector" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 
     android:text="@string/navigate" 
     android:visibility="gone" 
     android:textColor="@color/cups_black" /> 

</RelativeLayout> 

虽然MapFragment只包含一个SupportMapFramgnet在里面,

MapFragment Layout:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
android:id="@+id/mapview" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
map:cameraTargetLat="32.062505" 
map:cameraTargetLng="34.778651" 
map:cameraZoom="12" 
map:mapType="normal" 
map:uiZoomControls="false" 
map:uiRotateGestures="true" 
map:uiScrollGestures="true" 
map:uiZoomGestures="true" 
map:uiTiltGestures="false" /> 

我想用FragmentManager添加此片段,然后拿到 mapview这基本上是SupportMapFragment,所以我要做的就是实例:

getSupportFragmentManager().beginTransaction() 
    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right) 
    .add(R.id.content, MainFragment.create(), "Main Fragment") 
    .commit(); 

这个片段添加到Activity,然后我试图让mapview

smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview); 

,但我得到空在这里。

所以问题是:如何在这种情况下获得地图视图的实例?

在此先感谢。

+0

而不是supportmapfragment你应该有一个地图视图。我想你想要一个地图内的片段。 – Raghunandan

+0

实际上,这是一个实现,我从其他开发人员继承而来,大多数应用程序都依赖于此实现,所以我不能只是现在就更改它,并且允许使用片段级联。是否有办法以现在的方式获取它的实例? –

+0

没有太多改变片段的intead有地图视图并初始化片段视图 – Raghunandan

回答

3

因为MapFragment是一个子片段,所以您需要使用childFragmentManager而不是SupportFragmentManager。

所以,你需要做这样的事情:

MainFragment mainFragment = (MainFragment)getSupportFragmentManager.findFragmentById(R.id.content); 

MapFragment mapFragment = (MapFragment) mainFragment.getChildFragmentManager().findFragmentById(R.id.map_fragment); 

SupportMapFragment supportMap = (SupportMapFragment)mapFragment.getChildFragmentManager().findFragmentById(R.id.mapview); 

即未经测试的代码,而是应该指向你在正确的方向。

需要注意的是:你似乎有一个额外的孩子片段,你不需要。

MapFragment片段看起来像它只是一个包含SupportMapFragment的片段。 如果不需要,您应该尽量避免多余的儿童碎片。将MapFragment完全替换为SupportMapFragment,或者在MapFragment中自己实现mapview。

+0

不知道有一个ChildFragmentManager。我想这应该让我走向正确的方向。谢谢我会测试它并让你知道。是的,我知道地图,我明白这是如何解决的。 –