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



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

我正在尝试用课程片段替换课程片段。

课程片段中的代码是:

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" />

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

getFragmentManager()替换为getChildFragmentManager()

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

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

最新更新