[英]How to add fragment to existing fragment dynamically?
I want to add a fragment to an existing fragment on a button press (button sits on parent fragment) but I get an error: java.lang.ClassCastException: must implement OnFragmentInteractionListener. 我想在按下按钮时将片段添加到现有片段(按钮位于父片段上)但是我收到错误:java.lang.ClassCastException:必须实现OnFragmentInteractionListener。
What does that mean and why doesn't any of the example have it? 这是什么意思,为什么没有任何一个例子呢?
Parent fragment button press code: 父片段按钮按下代码:
Button interestedButton = (Button) myFragmentView.findViewById(R.id.interestedButton);
interestedButton.setOnClickListener(new View.OnClickListener() {
InterestedFormFragment interestedFormFragment = new InterestedFormFragment();
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.interestedFrag_container, interestedFormFragment, "INTERESTED_FORM");
fragmentTransaction.commit();
}
});
Parent fragment XML: 父片段XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_gravity="bottom"
android:id="@+id/buildingPageId">
<LinearLayout
android:orientation="vertical"
android:id="@+id/interestedFrag_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
...
The "interestedFrag_container" should hold the newly added child fragment. “interestedFrag_container”应该保存新添加的子片段。
My parent fragment isinflated and hold the button that should add the child. 我的父片段是infinflated并按住应该添加孩子的按钮。 What am I missing here? 我在这里错过了什么? Thank you! 谢谢!
Nested fragment are only added since API 17. 嵌套片段仅在API 17之后添加。
You can use getChildFragmentManager() to manage your nested fragments. 您可以使用getChildFragmentManager()来管理嵌套片段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.