通常,如果我们想去其他页面。我们使用代码
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity(i);
finish();
我混淆了用片段转到其他页面。
TEACHER_Class.java
public class TEACHER_Class extends Fragment {
Button tambahKelasButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.teacher_class, container, false);
tambahKelasButton = (Button) rootView.findViewById(R.id.tambah_kelas_button);
tambahKelasButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment FRAGMENT = new TEACHER_AddClass();
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//I think I get error in here
fragmentTransaction.replace(R.layout.teacher_class, FRAGMENT);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return rootView;
}
}
TEACHER_AddClass.java
public class TEACHER_AddClass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.teacher_add_class, container, false);
return rootView;
}
}
我想从 TEACHER_Class.java 转移到 TEACHER_AddClass.java
这是teacher_class.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center_horizontal"
android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin" android:background="#393f46"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/teacher_class_layout"
>
<!-- Login progress -->
<Button android:id="@+id/tambah_kelas_button"
android:layout_width="150dp" android:layout_height="40dp"
android:layout_marginTop="20dp" android:layout_marginLeft="100dp"
android:text="TAMBAH KELAS" android:background="#f8255f"
android:textSize="18sp" android:textColor="#FFFFFF"
android:textStyle="bold" />
<ScrollView android:id="@+id/insert_class_form" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="30dp">
<TableLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#999999"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="3">
<TextView
android:id="@+id/header_class_name"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text=" NAMA KELAS"
android:layout_marginLeft="0dp"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
<TextView
android:id="@+id/header_bank_soal"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="BANK SOAL"
android:layout_marginLeft="20dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
<TextView
android:id="@+id/header_resources"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="RES"
android:layout_marginLeft="30dp"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:textSize="18sp"
android:textColor="#f6f6f6"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
</ScrollView>
teacher_add_class.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin" android:background="#393f46"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/teacher_add_class_layout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD CLASS"
android:textSize="28sp"
android:textColor="#FFFFFF"
android:id="@+id/loginTittle"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:layout_marginTop="60dp"
/>
<EditText
android:id="@+id/fullname_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Full Name"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="@+id/shortname_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Short Name"
android:layout_marginBottom="30dp"
/>
<EditText
android:id="@+id/summary_edittext"
android:layout_width="330dp" android:layout_height="50dp"
android:textSize="18sp" android:textColor="#282727"
android:background="#FFFFFF" android:hint="Summary"
android:layout_marginBottom="30dp"
/>
<Button
android:id="@+id/add_class_button"
android:layout_width="100dp" android:layout_height="60dp"
android:layout_marginTop="0dp" android:layout_marginLeft="150dp"
android:text="ADD" android:background="#f8255f"
android:textSize="22sp" android:textColor="#FFFFFF"
android:textStyle="bold" />
</LinearLayout>
答案 0 :(得分:2)
FragmentManager fragmentManager = getActivity().getFragmentManager();
您应该使用getChildFragmentManager
,从片段
fragmentTransaction.replace(R.layout.teacher_class, FRAGMENT);
第一个参数必须是承载Fragment本身的R.id.
的id(ViewGroup
),而不是布局的id。
答案 1 :(得分:0)
我怀疑R.id.layout.teacher_class是你要删除的片段吗? replace函数的第一个参数是容纳片段的容器,而不是要替换的实际片段。
fragmentTransaction.replace([TARGET_CONTAINER], FRAGMENT);