2017-05-07 10 views
0

フラグメントに問題がありますが、その理由はわかりません。主な活動には2つの断片があります。一方の側(右)には断片の上にスピナーがあり、その断片にはどの断片がスピナーと置き換えられます。左側には、アレイアダプタを使用してデータを表示したリストビューが表示されています。フラグメントを置換すると別のフラグメントが削除されます

問題は、左側のフラグメントを置き換えるときはいつも、右側のフラグメントが消えて、なぜそれがそれをしているのかわかりません。

これは、左側フラグメントを交換する私のスピナーのコードされています。右側の断片は配列リストに新規追加されたデータを表示するには、次のスピナーにあるボタンを使用してリフレッシュ取得し、それが

personSelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

      if (position == 0) { 

       personType = "Student"; 

       StudentFragment studentFrag = StudentFragment.newInstance(); 
       getFragmentManager().beginTransaction() 
         .replace(R.id.form, studentFrag) 
         .commit(); 

      } else if (position == 1) { 

       personType = "Teacher"; 

       TeacherFragment teacherFrag = TeacherFragment.newInstance(); 
       getFragmentManager().beginTransaction() 
         .replace(R.id.form, teacherFrag) 
         .commit(); 

      } else if (position == 2) { 

       personType = "Administrator"; 

       AdminFragment adminFrag = AdminFragment.newInstance(); 
       getFragmentManager().beginTransaction() 
         .replace(R.id.form, adminFrag) 
         .commit(); 

      } // end if/else 

     } // end personSelection 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) {} 

    }); // end selection 

ですフラグメントが使用された時間だけです。

私は何を意味するのかを視覚的に示します。

enter image description here

  • をスピナーを選択した後:(更新ボタンは、この後のいずれでも動作しません)

enter image description here

始めたときにアプリケーションが正常に動作し

  • これは、主な活動のためのXMLです:

    <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 
    
        <LinearLayout 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:orientation="horizontal"> 
    
        <Spinner 
         android:layout_height="40dp" 
         android:id="@+id/person_spinner" 
         android:entries="@array/child" 
         android:layout_width="170dp" /> 
    
        <Button 
         android:text="Button" 
         android:id="@+id/refresh_button" 
         android:drawableStart="@android:drawable/stat_notify_sync" 
         android:drawableTint="#000000" 
         android:layout_width="45dp" 
         android:layout_height="50dp" /> 
        </LinearLayout> 
    
        <fragment 
        android:name="com....StudentFragment" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight=".4" 
        android:id="@+id/form" 
         tools:layout="@layout/student_fragment" /> 
    
    </LinearLayout> 
    
    <fragment 
        android:name="com....DetailFragment" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_weight=".7" 
        android:id="@+id/frame_detail" 
        tools:layout="@layout/fragment_detail"/> 
    

答えて

0

たぶん両方(左と右の断片)を交換してみてください動的に。 XMLレイアウトにフラグメントを定義しないでください。代わりに、2つのFrameLayoutsをフラグメントのコンテナとして使用し、必要に応じてコード内で必要なフラグメントを動的にアタッチします。

0

私はそれを修正しました。私はフラグメントを置き換えようとすべきではありません、これはそれを削除し、主な活動の全体のレイアウトを混乱させました。だから、私がしたのは、すべてのフラグメントをメインにまとめて、リニアレイアウト内の相対レイアウトに追加することでした。 onCreateメソッドでは、私はスピナの選択に応じて、私が望むものを表示し、他のものを隠すために可視性を変更しました。今、アプリ全体が問題なく動作します。

関連する問題