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

Detailed introduction of the use of fragments in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "detailed introduction to the use of fragments in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "detailed introduction to the use of fragments in Android"!

The use of Fragment

In fact, fragments are very simple, but there are so many random blog posts on the Internet that we sometimes feel confused. Today, let's briefly explain the use of fragments. There are two types of fragmentation: static fragmentation and dynamic fragmentation. Let's take a look at how to add fragmentation statically.

Add fragments statically

First of all, first of all, build two Layout files, that is, the fragment layout file, you may have found that Android Studio can directly and quickly create fragments, just like Activity, but this will generate a lot of useless code, so we still choose to create our own fragment layout.

Both layouts are simple. There is only one TextView displayed in the center. Paste the code below.

First layout file: fragment_first.xml

The second layout file, fragment_second.xml

Now that the layout files are finished, it's time for us to create their corresponding Fragment, that is, the background code. Two new classes, called FirstFragment and SecondFragment, are both inherited from Fragment. It should be noted that all the Fragment used in our tutorials are under the package android.support.v4.app.Fragment, which is more conducive to program compatibility.

Pasting the code for the two classes is also simple, just rewriting the onCreateView method to load different layout files.

Public class FirstFragment extends Fragment {private View view;// gets the layout file corresponding to the fragment, which is convenient for subsequent use / / remember to rewrite the onCreateView method @ Nullable@Overridepublic View onCreateView (LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {view = inflater.inflate (R.layout.fragment_first, container, false); / / to get the corresponding layout file return view;}} public class SecondFragment extends Fragment {private View view / / get the layout file corresponding to the fragment for subsequent use / / remember to rewrite the onCreateView method @ Nullable@Overridepublic View onCreateView (LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {view = inflater.inflate (R.layout.fragment_second, container, false); / / get the corresponding layout file return view;}}

Well, we've done the basic work, and now we use two Activity to show how to add fragments statically and dynamically.

To add a control statically, you need to use the fragment control and specify that its name is the Fragment you just created. Let's take a look.

First post the layout of the first Activity:

That button is used to jump to the next interface, Activity, which dynamically adds fragmentation cases, which can be ignored here.

Here we see that the two fragment specify name as FirstFragment and SecondFragment, that is, the two Fragment you just created, be sure to add the package name. By the way, there is another problem, that is, there is no preview in this way. If you want to preview, you need to add a line to the fragment tag:

Tools:layout= @ layout/ layout file name.

All right, statically adding fragments is done. What? It's that simple, right. It's as simple as that.

Add fragments dynamically

Dynamically add fragments we do not need to use a fragment control, but need to use a FrameLayout control, this is why, first of all, we all know that the controls in FrameLayout are displayed from the upper left corner, without position control, dynamically adding fragments is actually adding fragments to the container dynamically, while fragment controls can only be used to statically bind a fragment.

Post the layout of the second Activity first:

The two buttons above are used to load different fragments, and the FrameLayout below is the container where the fragments are displayed.

Don't say much nonsense, post the code:

Import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;public class SecondActivity extends AppCompatActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_second); / / load the first fragment findViewById (R.id.second_btnLoadFirst) .setOnClickListener (new View.OnClickListener () {@ Overridepublic void onClick (View view) {FragmentManager fragmentManager = getSupportFragmentManager (); FragmentTransaction transaction = fragmentManager.beginTransaction () when the first button is clicked Transaction.replace (R.id.second_fl, new FirstFragment (); transaction.commit ();}}); / / load the second fragment findViewById (R.id.second_btnLoadSecond) .setOnClickListener (new View.OnClickListener () {@ Overridepublic void onClick (View view) {getSupportFragmentManager (). BeginTransaction (). Replace (R.id.second_fl, new SecondFragment ()). Commit (); / / abbreviation}}) when the second button is clicked.

The getSupportFragmentManager method is used to get a fragment manager object (note that it is under the android.support.v4.app package when using this method), and then use this method to start a fragment transaction object, which is critical and can be used to dynamically add fragments, call its replace method, remove all other controls in the specified container, and then add new fragments. Here is to empty the control inside the R.id.second_f1, and then add a FirstFragment into it.

Remember to call the commit method to submit after replacement, or all your actions will not take effect.

At this point, I believe you have a deeper understanding of the "detailed introduction to the use of fragments in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 0

*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