2016-09-30 5 views
0

私はこのフォームなぜ私のフラグメントがお互いの上に現れるのですか?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/fragment1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/fragment2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
    </LinearLayout> 

</android.support.design.widget.CoordinatorLayout> 

の主なレイアウトを持っていると私は、次のXMLを使用する2つのフラグメントを呼び出すために、それらの両方に.replace()を呼び出すFragmentTransactionをしています:Fragment1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="true"> 

    ... 

</LinearLayout> 

フラグメント2.xml

なぜ2つの断片がお互いの上に現れるのですか? RecraglerViewがあるFragment2のすぐ上にFragment1を表示します。

編集:置換コード:

fragment1 = Fragment1.newInstance(); 
fragment2 = Fragment2.newInstance(); 

FragmentManager fragmentManager = getChildFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

fragmentTransaction.replace(R.id.fragment1, fragment1, "fragment1"); 
fragmentTransaction.replace(R.id.fragment2, fragment2, "fragment2"); 

fragmentTransaction.commit(); 
+0

これは間違っているのですか、置き換え以外のものを使用する必要がありますか? – KaliMa

+0

Fragmentをどのように追加したのかを示すコードを表示し、さらに明確にするためのスクリーンショットも表示します。 – Enzokie

+0

@Enzokieいくつかのコードを追加しました – KaliMa

答えて

1

あなたはCoordinatorLayout内の2つのレイアウトを置くためです。 CoordinatorLayoutは、RelativeLayoutのように動作します。代わりにこれを試してください。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <FrameLayout 
     android:id="@+id/fragment1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
     <FrameLayout 
     android:id="@+id/fragment2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 
+0

RelativeLayoutのようなものであれば、それは私に全体の "layout_above"または "below"のことをさせてくれませんでしたか? – KaliMa

+0

あなたの修正プログラムはbtwで動作しました。 – KaliMa

+0

私はそれが 'RelativeLayout'のように振る舞いますが、それが本質的ではないことを意味します。別の目的に使用されます。 – Joshua

0

あなたは活動のレイアウトに直接断片をインポートすることができます。

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    class="com.packacge.Fragment1"/> 

<fragment 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    class="com.packacge.Fragment2"/> 
+0

はい、後でそれらの領域の他のフラグメントを交換したいかもしれません。今、私はちょうどこの2つのケースが動作するようにしようとしています – KaliMa

関連する問題