1, Method and steps of static registration of fragment
1. Create a static fragment.java file to inherit fragment class and a static_fragment.xml file to complete fragment layout. Overloading oncreateview in StaticFragment.java ( )Method by calling inflate. Inflate ( )Method and pass in the layout resource ID to generate the view resource of fragment, bind the relevant components in static_fragment.xml and implement its functions. The implementation code is as follows:
static_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".StaticFragment" android:orientation="vertical"> <Button android:id="@+id/btn_fm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is fragment Static registration" android:textAllCaps="false"> </Button> <EditText android:id="@+id/et_fm" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Please enter what you want to change:"> </EditText> </LinearLayout>
StaticFragment.java:
package com.example.myapplication; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; public class StaticFragment extends Fragment { private Button mBtnFm; private EditText mEtFm; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //The view resource of fragment is directly through the call to inflate. Inflate ( )Method and passed in the generated by the layout resource ID. View v = inflater.inflate(R.layout.static_fragment, container,false); mEtFm = v.findViewById(R.id.et_fm); mBtnFm = v.findViewById(R.id.btn_fm); mBtnFm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mBtnFm.setText(mEtFm.getText().toString()); } }); return v; } }
2. Bind the fragment layout file in the main layout activity main.xml file. The implementation code is as follows:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is the main layout" android:textColor="@color/colorAccent" android:textSize="30sp"> </TextView> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Below is fragment Layout" android:textColor="@color/colorPrimaryDark" android:textSize="30sp"> </TextView> <fragment android:id="@+id/static_fm" android:name="com.example.myapplication.StaticFragment" android:layout_width="match_parent" android:layout_height="wrap_content"> </fragment> </LinearLayout>
Note: add the fragment node to the layout file, and the name attribute must fill in the complete path
MainActivity.java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Demonstration:
Android - fragment static registration demo
2, Methods and steps of dynamic registration and creation of fragment
1. Create a new project, create two Fragment inheritance classes MyFragment1.java and MyFragment2.java, and then create two layout files fragment1.xml and fragment2.xml. The detailed code is as follows:
fragment1.xml:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MyFragment1" android:gravity="center" android:background="@color/colorPrimaryDark"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="@string/hello_blank_fragment" android:textSize="30sp" android:textAllCaps="false" android:textColor="#F70505"> </TextView> </LinearLayout>
MyFragment1.java:
package com.example.myapplication; import android.content.Context; import android.net.Uri; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment1, container, false); } }
fragment2.xml:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MyFragment2" android:gravity="center" android:background="@color/colorAccent"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="@string/hello_blank_fragment" android:textSize="30sp" android:textAllCaps="false" android:textColor="#03FAE3"> </TextView> </LinearLayout>
MyFragment2.java:
package com.example.myapplication; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class MyFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment2, container, false); } }
The above code is not much different from static creation.
2. Adding fragment to activity in the form of code requires calling fragment manager directly in activity.
FragmentManager fm = getSupportFragmentManager();
Then through the code block:
FragmentTransaction ts = fm.beginTransaction(); Fragment mfg1 = new MyFragment1(); ts.add(R.id.fragment_container,mfg1); ts.commit();
Commit a fragment transaction. Its core is ts.add( Method.
The detailed code is as follows:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity"> <LinearLayout android:id="@+id/linear" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_alignParentBottom="true"> <Button android:id="@+id/btn_dy1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment1" android:textColor="@color/colorAccent" android:textSize="30sp"> </Button> <Button android:id="@+id/btn_dy2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment2" android:textColor="@color/colorPrimaryDark" android:textSize="30sp"> </Button> </LinearLayout> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content"> </FrameLayout> </RelativeLayout>
Note: generally, the FrameLayout layout is used to carry the fragment module
MainActivity.java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button mBtnDy1; private Button mBtnDy2; FragmentManager fm; Fragment mfg1; Fragment mfg2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fm = getSupportFragmentManager(); mBtnDy1 = findViewById(R.id.btn_dy1); mBtnDy2 = findViewById(R.id.btn_dy2); mBtnDy1.setOnClickListener(this); mBtnDy2.setOnClickListener(this); } @Override public void onClick(View v) { clearSelection();//Clear button status FragmentTransaction ts = fm.beginTransaction(); hideFragments(ts); switch (v.getId()){ case R.id.btn_dy1: mBtnDy1.setBackgroundColor(0xff0000ff); if(mfg1 == null){ mfg1 = new MyFragment1(); ts.add(R.id.fragment_container,mfg1); }else { ts.show(mfg1); } break; case R.id.btn_dy2: mBtnDy2.setBackgroundColor(0xff0000ff); if(mfg2 == null){ mfg2 = new MyFragment2(); ts.add(R.id.fragment_container,mfg2); }else { ts.show(mfg2); } break; default: break; } ts.commit(); } // Hide all fragments. private void hideFragments(FragmentTransaction transaction) { if (mfg1 != null) { transaction.hide(mfg1); } if (mfg2 != null) { transaction.hide(mfg2); } } // Clear all selected states. private void clearSelection() { mBtnDy1.setBackgroundColor(0xffffffff); mBtnDy2.setBackgroundColor(0xffffffff); } }
Demonstration:
Android - dynamic registration demonstration of fragment