2017-01-29 9 views
0

私はactivityの地図を表示しています。ナビゲーション・ドロワーの項目を別の断片でクリックすると変更できます。コンテンツをフラグメントに置き換えることはできません

私は次のメインXML持っている:私は断片でマップを交換しようと

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/my_content" 
android:background="@color/window_background_color"> 


<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_below="@+id/adView"/> 


</RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.main.activities.Activity_main"> 

<include layout="@layout/my_bar" /> 

<android.support.design.widget.NavigationView 
    android:layout_marginTop ="45dp" 
    android:id="@+id/navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:menu="@menu/navigation_menu" 
    app:itemIconTint="#000000" 
    android:background="#FFFFFF" 
    app:itemTextColor="#000000" 
    app:headerLayout="@layout/navigation_header"> 
</android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

バーのレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
xmlns:app="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:id="@+id/my_bar" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<!-- Toolbar --> 

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@color/primary_color" 
    android:titleTextAppearance="@color/text_and_icon_color" 
    app:popupTheme="@style/Theme.AppCompat" 
    android:elevation="1dp" 
    android:textAlignment="center" 
    android:layout_height="45dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="My app" 
     android:layout_gravity="center" 
     android:id="@+id/toolbar_title" 
     android:layout_weight="1" 
     android:textSize="24sp" 
     android:textColor="@android:color/white" /> 

</android.support.v7.widget.Toolbar> 

<include layout="@layout/my_content" /> 

</LinearLayout> 

私のコンテンツを

<FrameLayout 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.main.activities.catalog"> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

私はこのコードを呼び出す:

catalog fragment2 = new catalog(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.my_content, fragment2, "fragment2"); 
     fragmentTransaction.commit(); 

しかし、私のマップが新しいフラグメントと交換されていません。私が間違っているところ?

+0

フラグメントを変更するコードを含むクラスとメソッドはありますか?ナビゲーションドロワーのアイテムをクリックするとどうなりますか? –

+3

レイアウトで指定された静的 'Fragment'は動的に削除/置換できません。 –

答えて

2

@MikeMはあなたの質問にコメントしているように、my_content.xmlにXMLで直接指定された静的フラグメントが含まれているという問題があります。 FragmentManagerは、このようなフラグメントを実行時に動的に置き換えることはありません。代わりにfragmentタグを削除し、マップフラグメントをonCreate()に動的に追加する必要があります。通常これはFrameLayoutで行われますが、RelativeLayoutでも動作すると思います。

詳細については、Fragment Developer Guideを参照してください。

+0

@Mike Mのコメントはありますか? – AnixPasBesoin

+0

@AnixPasBesoin私は彼のコメントを見る前にこれをタイプしました。しかし、私はrefを追加することができます。 –

+0

私の問題は、私のコンテンツレイアウトの地図断片の下にもっと多くのレイアウトがあることです。アクティビティのマップフラグメントを作成し、レイアウトの最初に挿入することはできますか? – gogoloi

関連する問題