Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create static Registration and dynamic Registration in Android Fragment

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to create static registration and dynamic registration in Android Fragment", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to create static registration and dynamic registration in Android Fragment" article.

I. methods and steps for the creation of fragment static registration

1. Create a StaticFragment.java file that inherits the Fragment class and a static_fragment.xml file to complete the layout of the fragment. Overload onCreateView (…) in StaticFragment.java Method, by calling inflater.inflate (...) Method and input the layout resource ID to generate the view resource of the fragment, bind the relevant components in the static_fragment.xml, and then implement its function. The implementation code is as follows:

Static_fragment.xml

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 inflater.inflate (…) Method and pass in the layout resource ID generated. 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;}}

two。 Bind the fragment layout file in the main layout activity_main.xml file.

The implementation code is as follows:

Activity_main.xml

Note: add a 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);}}

Demo:

2. The methods and steps of creating dynamic registration 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

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

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.

two。 Adding fragment to activity in the form of code requires a direct call to FragmentManager in activity.

FragmentManager fm = getSupportFragmentManager ()

Then through the code block:

FragmentTransaction ts = fm.beginTransaction (); Fragment mfg1 = new MyFragment1 (); ts.add (R.id.fragmentations container.mfg1); ts.commit ()

Commit a fragment transaction. Its core is ts.add (…) Method.

The detailed code is as follows:

Activity_main.xml:

Note: fragment modules are generally hosted by FrameLayout layout

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.fragmentationcontainer.mfg1);} else {ts.show (mfg1);} break; case R.id.btn_dy2: mBtnDy2.setBackgroundColor (0xff0000ff) If (mfg2 = = null) {mfg2 = new MyFragment2 (); ts.add (R.id.fragmentationcontainer.mfg2);} else {ts.show (mfg2);} break; default: break;} ts.commit ();} / / sets all Fragment to be hidden. Private void hideFragments (FragmentTransaction transaction) {if (mfg1! = null) {transaction.hide (mfg1);} if (mfg2! = null) {transaction.hide (mfg2);}} / / clears all selected states. Private void clearSelection () {mBtnDy1.setBackgroundColor (0xffffffff); mBtnDy2.setBackgroundColor (0xffffffff);}}

Demo:

The above is about the content of this article on "how to create static registration and dynamic registration in Android Fragment". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 257

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report