Загрузка другого фрагмента во фрагмент не работает
Я пытаюсь загрузить фрагмент в другой фрагмент. Но макет не загружается. Я помещаю тостовое сообщение во второй фрагмент, который появляется, но расположение не изменяется. Это вид навигации. Поэтому я не хочу начинать новую деятельность. Я всегда хочу, чтобы отображался вид навигации, но мне нужно, чтобы появилось много экранов на основе нажатия кнопки
Я пытаюсь заменить фрагмент курса фрагментом урока.
Код в фрагменте курса:
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 ответа
Мы должны использовать getChildFragmentManager()
заменить фрагменты внутри фрагмента
замещать getFragmentManager()
с getChildFragmentManager()
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.coursefragment, new lessonFragment(), "NewFragmentTag");
ft.commit();
ft.addToBackStack(null);
Замените ваш getFragmentManager() на getChildFragmentManager(). Потому что, если вы хотите открыть дочерний фрагмент из фрагмента, вам нужно использовать дочерний менеджер фрагментов.