本文介绍了从片段获取地图视图实例:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MainFragment 是由两个片段,一个 MapFragment ListFragment ,这是它的布局:

I have a MainFragment that consist of two fragments, a MapFragment and a ListFragment and this is its layout:

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布局:

<?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 ,然后得到的实例
图形页面这基本上是 SupportMapFragment ,所以我做的是:

I want to add this fragment using the FragmentManager and then get the instance of themapview which is basically the SupportMapFragment, so what I do is:

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();

这个片段添加到活动,然后我试图让图形页面

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

但是我得到空在这里。

But I get Null here.

所以,问题是:我怎样才能获得地图视图的实例,在这种情况下

So the Question is: How can I get the instance of the map view in this case?

先谢谢了。

推荐答案

由于该MapFragment是一个孩子的片段,你需要使用childFragmentManager代替SupportFragmentManager。

Because the MapFragment is a child fragment, you need to use the childFragmentManager instead of SupportFragmentManager.

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

So you will need do something like:

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

这是未经测试code,但应该指向你在正确的方向。

That is untested code, but should point you in the right direction.

有得但请注意:你似乎有你并不需要一个额外的子片段

Something to note though: you seem to have an extra child fragment which you don't need.

该MapFragment片段看起来像它只是一个包含SupportMapFragment片段。
你应该尽量避免过多的孩子片段,如果他们是没有必要的。无论是与SupportMapFragment更换MapFragment完全,或实现自己的MapView在MapFragment。

The MapFragment fragment looks like its just a fragment that contains a SupportMapFragment.You should try to avoid excess child fragments if they aren't necessary. Either replace the MapFragment with the SupportMapFragment entirely, or implement the mapview yourself in your MapFragment.

这篇关于从片段获取地图视图实例:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 01:41