2015-02-06 70 views
0

我在我的地圖應用程序中使用導航抽屜,我設法在主頁面上顯示地圖。我遇到的問題是我無法將地圖片段(佈局「xml」)連接到MapsActivity(java文件)。有沒有辦法顯示一個片段內的活動

我試圖使用MapsActivity extends Fragment而不是MapsActivity extends FragmentActivity,但當然這是行不通的。

主XML佈局:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<!-- As the main content view, the view below consumes the entire 
    space available using match_parent in both dimensions. --> 
<FrameLayout android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


<!-- *****The problem I am having is in the following fragment could not link it to the MapsActivity--> 
<fragment 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/map" 
     tools:context=".MapsActivity" 
     android:name="com.google.android.gms.maps.SupportMapFragment"/> 


</FrameLayout> 


<!-- android:layout_gravity="start" tells DrawerLayout to treat 
    this as a sliding drawer on the left side for left-to-right 
    languages and on the right side for right-to-left languages. 
    If you're not building against API 17 or higher, use 
    android:layout_gravity="left" instead. --> 
<!-- The drawer is given a fixed width in dp and extends the full height of 
    the container. --> 
<fragment android:id="@+id/navigation_drawer" 
    android:layout_width="@dimen/navigation_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:name="com.example.famfelimban.mydiet.NavigationDrawerFragment" 
    tools:layout="@layout/fragment_navigation_drawer" /> 

然而,我有其工作正常 activity_maps.xml地圖其工作正常與MapsActivity

<fragment 
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" 
android:id="@+id/map" 
tools:context=".MapsActivity" 
android:name="com.google.android.gms.maps.SupportMapFragment"/> 
一個單獨的XML佈局

MapsActivity

public class MapsActivity extends FragmentActivity { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 

    } 

    private void setUpMapIfNeeded() { 

    } 

    private void setUpMap() { 

    } 
} 

回答

0

你想在這裏做的是端口的Activity代碼爲Fragment。這並不難,一個片段的生命週期幾乎相同。唯一的區別是不需要抓取和修改onCreate中的View,您需要在onCreateView中執行該操作,而需要實施static newInstance()方法。

將xml中的FrameLayout留空。只有FragmentManager才能將片段膨脹成爲。兩處更改代碼

Activity

@Override 
protected void onStart() { 
    super.onStart(); 

    Fragment mapFragment = MapFragment.newInstance(/*args*/); 
    getFragmentManager() //you may need getSupportFragmentMananger() 
      .beginTransaction() 
      .replace(R.id.container, mapFragment) 
      .commit() 
    //this inflates the Fragment into the FrameLayout 
} 

,並作出Fragment,其功能就像你Activity版本

public class MapFragment extends Fragment { 
    ... 
    public MapFragment() {/*should be empty*/} 
    ... 
    public static MapFragment newInstance(/*args*/) { 
     Fragment fragment = new MapFragment(); 

     Bundle args = new Bundle(); 
     //put whatever information you need for your activity to run here 
     fragment.setArguments(args); 
     return fragment; 
    } 
    ... 
    @Overide 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
     //this basically takes the place of onCreate in the Activity version 
     View root = inflater.inflate(R.layout.fragment_map, parent); 

     View yourView = root.findViewById(R.id.your_view); 
     ... 
     return root; 
    } 
    ... 
} 

好運。

+0

感謝您的回覆,那麼您的意思是我只在我的Activity中擁有onStart(),然後將Activity中的所有方法實現爲片段? – Feras 2015-02-07 17:08:26

+0

'onCreate()'仍然存在。 'setContentView()'仍然需要調用,你可以在'onCreate()'中做'Fragment'事務,但'Activity'的主要工作是啓動'Fragment',它完成所有繁重的工作。 – seiterm 2015-02-09 13:36:03