在片段中加载另一个片段不起作用

时间:2017-05-10 12:12:30

标签: android navigation-drawer

我试图在另一个片段中加载一个片段。但布局没有加载。我在第二个片段中放了一个Toast消息,但是布局没有改变。这是一个导航视图。所以基本上我不想开始新的活动。我总是希望导航视图出现,但我需要根据按钮单击显示许多scree

我试图用lessonfragment替换courseFragment。

courseFragment中的代码是:

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

费用片段的代码是:

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,名为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" />

2 个答案:

答案 0 :(得分:0)

我们应该使用getChildFragmentManager()来替换片段中的片段

getFragmentManager()替换为getChildFragmentManager()

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

答案 1 :(得分:0)

用getChildFragmentManager()替换你的getFragmentManager()。 因为如果要从片段中打开子片段,则需要使用子片段管理器。