2017-05-10 3 views
0

他のフラグメントにフラグメントをロードしようとしています。しかし、レイアウトはロードされていません。私は2番目の断片にトーストメッセージを入れましたが、それは表示されますが、レイアウトは変わりません。これはナビゲーションビューです。だから基本的に私は新しい活動を始めたいとは思わない。私は常にナビゲーションビューを表示したいのですが、ボタンクリックに基づいて表示するには多くの画面が必要です他のフラグメントをフラグメントにロードしていない

私はcourseFragmentをlessonfragmentに置き換えようとしています。

courseFragmentのコードがある:

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      ft.replace(R.id.coursefragment, new lessonFragment(), 
"NewFragmentTag"); 
      ft.commit(); 
      ft.addToBackStack(null); 

料断片のコードは次のとおりfragmnet_course.xml

LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:tools="http://schemas.android.com/tools" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:background="#04182f" 
android:id="@+id/coursefragment" 
android:orientation="vertical" 
tools:context="com.example.apple.project4.CoursesFragment" /> 
<Space 
    android:layout_width="match_parent" 
    android:layout_height="60dp" /> 
<TextView 
    android:layout_width="260dp" 
    android:layout_height="60dp" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:clickable="true" 
    android:id="@+id/lesson0" 
    android:background="@color/one" 
    android:layout_gravity="center" 
    android:textSize="20dp" 
    android:text="Lessons0"> 

<Space 
    android:layout_width="match_parent" 
    android:layout_height="20dp" /> 
<TextView 
    android:layout_width="260dp" 
    android:layout_height="60dp" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:id="@+id/lesson2" 
    android:background="@color/one" 
    android:layout_gravity="center" 
    android:textSize="20dp" 
    android:text="Lessons1"/> 
<Space 
    android:layout_width="match_parent" 
    android:layout_height="20dp" /> 
<TextView 
    android:layout_width="260dp" 
    android:layout_height="60dp" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:background="@color/one" 
    android:layout_gravity="center" 
    android:textSize="20dp" 
    android:id="@+id/lesson2" 
    android:text="Lessonsq2"/> 
<Space 
    android:layout_width="match_parent" 
    android:layout_height="20dp" /> 
<TextView 
    android:layout_width="260dp" 
    android:layout_height="60dp" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:background="@color/one" 
    android:layout_gravity="center" 
    android:textSize="20dp" 
    android:id="@+id/teachertraining" 
    android:text="Training of Teachers"/> 
<Space 
    android:layout_width="match_parent" 
    android:layout_height="20dp" /> 
<TextView 
    android:layout_width="260dp" 
    android:layout_height="60dp" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:background="@color/one" 
    android:layout_gravity="center" 
    android:textSize="20dp" 
    android:id="@+id/childrenscourse" 
    android:text="Children's courses"/> 
<FrameLayout 
    android:id="@+id/content_frame1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
呼ば

public class lessonFragment extends Fragment { 

ViewGroup rootView; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    //returning our layout file 
    //change R.layout.yourlayoutfilename for each of your fragments 
    rootView = (ViewGroup) inflater 
      .inflate(R.layout.cours, container, false); 
    ButterKnife.bind(this, rootView); 

    Toast.makeText(getActivity(),"You open lesson",Toast.LENGTH_SHORT).show(); 

    return rootView; 
} 


@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    //you can set the title for your toolbar here for different fragments different titles 
    getActivity().setTitle("Lessons"); 
} 

}

CourseFragmentのXML

答えて

0

我々は)getChildFragmentManager(と)(あなたのgetFragmentManagerを交換しgetChildFragmentManager()

final FragmentTransaction ft = getChildFragmentManager().beginTransaction(); 
ft.replace(R.id.coursefragment, new lessonFragment(), "NewFragmentTag"); 
ft.commit(); 
ft.addToBackStack(null); 
0

getFragmentManager()を置き換えフラグメント

内のフラグメントを置き換えるためにgetChildFragmentManager()を使用する必要があります。 フラグメントから子フラグメントを開く場合は、子フラグメントマネージャを使用する必要があります。

関連する問題