We can write a dynamic replacement buttons used APP fragments, the first fragment is first displayed on the home page, click on the button to the second fragment can be replaced, or deleted already replaced a second debris.
A .MainActivity.java
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends FragmentActivity { public MainActivity() { Log.e("TAG", "MainActivity().."); } @Override protected void onCreate(Bundle savedInstanceState) { Log.e("The TAG", "the MainActivity onCreate () .." ); Super .onCreate (savedInstanceState); the setContentView (R.layout.activity_main); // override onCreate () method // Create Object Fragment Final MyFragment1 fragment1 = new new MyFragment1 ( ); // get FragmentManager FragmentManager Manager = getSupportFragmentManager (); // get FragmentTransacation FragmentTransaction Transaction = manager.beginTransaction (); // add object and submit Fragment transaction.add (R.id.ll_main, fragment1) .commit ( ); Button1 the Button =(Button)findViewById(R.id.fragment_1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showFragment2(); } }); Button button2=(Button)findViewById(R.id.fragment_2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { deleteFragment2(); } }); } private MyFragment2 fragment2; public void showFragment2 () { // create objects Fragment fragment2 = new new MyFragment2 (); // get FragmentManager FragmentManager Manager = getSupportFragmentManager (); // get FragmentTransacation FragmentTransaction Transaction = manager.beginTransaction (); // current operation added to the back unstack, back to back so that clicking on a state transaction.addToBackStack ( null ); // replace Fragment object and submit transaction.replace (R.id.ll_main, fragment2) .commit (); } public void deleteFragment2 () { / /Obtained FragmentManager FragmentManager Manager = getSupportFragmentManager (); // get FragmentTransacation FragmentTransaction Transaction = manager.beginTransaction (); // remove Fragment object and submit transaction.remove (fragment2) .commit (); } }
二.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/fragment_1" android:text="切换至第二个碎片" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/fragment_2" android:text="删除第二个碎片" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/ll_main" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> </LinearLayout>
Good interface prepared as shown below:
Three .MyFragment1.java
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyFragment1 extends Fragment { public MyFragment1() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_my_fragment1, container,false); } }
Four .MyFragment2.java
import android.os.Bundle; import androidx.fragment.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyFragment2 extends Fragment { public MyFragment2() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_my_fragment2, container,false); } }
Five .fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".MyFragment1"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text the FrameLayout<//>= "This is the first fragment" >
Six .fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".MyFragment2"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text the FrameLayout<//>= "This is a second fragment" >
Complete