Fragment II: Basic Usage

Keywords: Android Fragment xml REST


Catalog (?)[-]

  1. A Static Add Fragment
  2. Two Dynamic Addition Fragment s

Foreword: There is still no foreword... The article is written too fast, the life has too many rules and regulations, the preface does not know what to write...


Related articles:

1,One of the Detailed Explanations of Fragment - Overview
2,Fragment Detailed Explanation II - Basic Usage
3,Fragment Detailed Explanation 3 - Managing Fragment (1)
4,Fragment IV: Managing Fragment (2)
5,Fragment Detailed Explanation V: Parametric Transfer Between Fragments
6. Fragment Detailed Explanation 6: How to Monitor Back Events in Fragment and How to Preserve Fragment Status


In the last article, we briefly talked about what Fragment is used for and the life cycle. In this article, we use examples to see how we use Fragment in our code.

Here we use all of them. Android - The Fragment in support-v4.jar package does not need the system's own Fragment; the two are basically the same, but the relative function in V4 package is more powerful.

I. Static Fragment Addition

Create a new project, harvicBlog2Static, in which add a layout: fragment1.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="match_parent"    
  3.     android:layout_height="match_parent"    
  4.     android:background="#00ff00" >    
  5.     
  6.     <TextView    
  7.         android:layout_width="wrap_content"    
  8.         android:layout_height="wrap_content"    
  9.         android:text="This is fragment 1"    
  10.         android:textColor="#000000"    
  11.         android:textSize="25sp" />    
  12.     
  13. </LinearLayout>    
As you can see, this layout file is very simple, with only one Linear Layout, which has a TextView added. As we did, we created a new fragment 2.xml:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="#ffff00" >  
  5.   
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="This is fragment 2"  
  10.         android:textColor="#000000"  
  11.         android:textSize="25sp" />  
  12.   
  13. </LinearLayout>  
Then create a new class Fragment 1, which inherits from Fragment:
  1. import android.os.Bundle;  
  2. import android.support.v4.app.Fragment;  
  3. import android.view.LayoutInflater;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6.   
  7. /** 
  8.  * Created by harvic on 2015/4/5. 
  9.  */  
  10. public class Fragment1 extends Fragment {  
  11.     @Override  
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  13.         return inflater.inflate(R.layout.fragment1, container, false);  
  14.     }  
  15. }  
Note the Fragment in the V4 package used! The main code here is to load and return the fragment1.xml layout file we just wrote. In the same way, let's write Fragment2:
  1. import android.os.Bundle;  
  2. import android.support.v4.app.Fragment;  
  3. import android.view.LayoutInflater;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6.   
  7. /** 
  8.  * Created by harvic on 2015/4/5. 
  9.  */  
  10. public class Fragment2 extends Fragment {  
  11.     @Override  
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  13.         return inflater.inflate(R.layout.fragment2, container, false);  
  14.     }  
  15. }  
Then open or create activity_main.xml as the layout file of the main Activity, add two Fragment references to it, and use the android:name prefix to refer to the specific Fragment:
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:baselineAligned="false" >  
  5.   
  6.     <fragment  
  7.         android:id="@+id/fragment1"  
  8.         android:name="com.harvic.com.harvicblog2.Fragment1"  
  9.         android:layout_width="0dip"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_weight="1" />  
  12.   
  13.     <fragment  
  14.         android:id="@+id/fragment2"  
  15.         android:name="com.harvic.com.harvicblog2.Fragment2"  
  16.         android:layout_width="0dip"  
  17.         android:layout_height="match_parent"  
  18.         android:layout_weight="1" />  
  19.   
  20. </LinearLayout>  
As for MainActivity, because of the V4 package we use, we must derive MainActivity from FragmentActivity, otherwise we can't start the program at all! Because the Activity of the system can only be used to hold fragments that come with the system, but not fragments in the V4 package, because the Activity of the system can not recognize fragments in the V4 package, because it is not a piece of code at all! If you don't use V4 packages, you don't have to derive MainActivity from FragmentActivity by using Fragmentation that comes with the system.
  1. import android.os.Bundle;  
  2. import android.support.v4.app.FragmentActivity;  
  3.   
  4. public class MainActivity extends FragmentActivity {  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.     }  
  11. }  
The results are as follows:

The source code is given at the bottom of the article.

2. Dynamic Fragment Addition

You've learned how to use Fragment in XML, but that's just the simplest function of Fragment. The real power of Fragment is that it can be added to Activity dynamically, so that's what you have to master. When you learn to add Fragments to Activity while the program is running, the interface of the program can be customized more diversified. Now let's take a look at how to add fragments dynamically.

Or to modify it on the basis of the previous section, open activity_main.xml, delete all the code, and change it to the following:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:baselineAligned="false" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn_show_fragment1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="display Fragment1"/>  
  12.   
  13.     <Button  
  14.         android:id="@+id/btn_show_fragment2"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="display Fragment2"/>  
  18.   
  19.     <FrameLayout  
  20.         android:id="@+id/fragment_container"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="match_parent"/>  
  23.   
  24. </LinearLayout>  
There are two buttons and a FrameLayout layout on the main interface. These two buttons are used to load instances of Fragment1 and Fragment2 in this FrameLayout, respectively. The results are as follows:
(The background of fragment1 was changed to purple because of the problem of green when recording GIF)

The rest of the code did not move, mainly in MainActivity, when clicking the two buttons to do the processing:

  1. public class MainActivity extends FragmentActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.   
  8.         Button btnLoadFrag1 = (Button)findViewById(R.id.btn_show_fragment1);  
  9.         btnLoadFrag1.setOnClickListener(new View.OnClickListener() {  
  10.             @Override  
  11.             public void onClick(View v) {  
  12.                 FragmentManager manager = getSupportFragmentManager();  
  13.                 FragmentTransaction transaction = manager.beginTransaction();  
  14.                 Fragment1 fragment1 = new Fragment1();  
  15.                 transaction.add(R.id.fragment_container, fragment1);  
  16.                 transaction.commit();  
  17.             }  
  18.         });  
  19.   
  20.         Button btnLoagFrag2 = (Button)findViewById(R.id.btn_show_fragment2);  
  21.         btnLoagFrag2.setOnClickListener(new View.OnClickListener() {  
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 FragmentManager manager = getSupportFragmentManager();  
  25.                 FragmentTransaction transaction = manager.beginTransaction();  
  26.                 Fragment2 fragment2 = new Fragment2();  
  27.                 transaction.add(R.id.fragment_container, fragment2);  
  28.                 transaction.commit();  
  29.             }  
  30.         });  
  31.     }  
  32. }  
Looking at the above code, it's easy to understand that when you click on the button, you do something similar:
  1. FragmentManager manager = getSupportFragmentManager();  
  2. FragmentTransaction transaction = manager.beginTransaction();  
  3. Fragment1 fragment1 = new Fragment1();  
  4. transaction.add(R.id.fragment_container, fragment1);  
  5. transaction.commit();  
Dynamic Fragment ation is divided into four steps:
  • 1. Get Fragment Manager, in V4 package through getSupportFragment Manager, in the system native Fragment is obtained through getFragment Manager.
  • 2. Open a transaction by calling the beginTransaction method.
  • 3. Fragment is added to the container, which is usually implemented by the add or replace method. The id and fragment instances of the container need to be passed in.
  • 4. Submit the transaction and call the commit method to commit.  
It doesn't matter that we don't understand the operation of fragments in this part. In the next section, we will talk about the management of fragments in detail.


If this article helps you, remember to pay more attention to it.

Source address: http://download.csdn.net/detail/harvic880925/8572761

Please respect the copyright of the originator. Please indicate the origin when reproducing: http://blog.csdn.net/harvic880925/article/details/44927363, Thank you!

Posted by classix16 on Wed, 17 Apr 2019 13:00:35 -0700