Fragment Introductory learning course in Android app development _android

Source: Internet
Author: User

A new concept called fragment was introduced on the Android3.0. It has its own layout files, can be arranged as a component, or can be combined to achieve different layout display. Using fragment, you can reuse code and meet the needs of different device sizes. Fragment cannot exist alone, only in activity, and an activity can have multiple fragment. It is important that fragment can be used with other components in the activity without rewriting the interfaces of all the activity. So using fragment can do this to complete the "Master interface-detailed interface" app requirement in the example above.

This is shown on the phone:

And on the tablet is this:

On a small-screen device, an activity usually occupies the entire screen while displaying various UI view components. An activity is actually a container for the view. Then, when an activity is shown on a large-screen device, such as a tablet, it always seems a bit inappropriate. Because the screen is so large that all the UI components in the activity are filled with the entire screen, the hierarchy of the view is complex. A better approach would be to use a "lightweight" activity in which each "lightweight" activity contains its own view and does not interfere with each other. During run time, depending on the direction and size of the screen, an activity can contain one or more "lightweight" activity. In the Android3.0 version, this "lightweight" activity is called fragment.

How to create a fragment

Now that we know the life cycle of fragment, we need to know how to create a fragment and bind to the activity, The first thing to do is to inherit android.app.Fragment to write a fragment, assuming our fragment is called Fragment1, created and defined as follows:

public class Fragment1 extends Fragment {
...
}

As we said above, fragment can only exist in activity, so we have to define it somewhere, there are two ways:
-Defined directly in the XML layout file;
-Define a placeholder in the XML layout file and then dynamically manipulate fragment in the activity;

The way we define fragment affects its lifecycle, because in the first case the Oninflate method is invoked, and in the second case its lifecycle begins with the Onattach method.

If we define fragment in an XML file, we need to:

<fragment android:id= "@+id/f1"
            class= "Com.survivingwithandroid.fragment.Fragment1"
       android:layout_ Width= "Match_parent"
       android:layout_height= "20DP"/>

However, if we use placeholders in XML, we need to do some more work.

Layout Framework and Fragment

If we define fragment in an XML layout file, we cannot freely and dynamically modify the fragment, and there are other ways to manipulate it more flexibly: when used, it needs to be defined in an XML file:

<framelayout android:id= "@+id/fl1"
       android:layout_width= "match_parent"
       android:layout_height= "200DP" />

There is still a bit of work to do in the activity, because we have to manually initialize the fragment and then "Insert" it into framelayout.

public class Mainactivity extends activity {
 
@Override
protected void onCreate (Bundle savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_main);
 
  Fragment2 F2 = new Fragment2 ();
  Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
  Ft.replace (R.ID.FL1, F2);
  Ft.commit ();
}


Example

Fragment can be imagined as another form of activity. You create fragments to include UI components, just as you would create activities. However, fragment is always embedded in the activity.

Here's an example to look at the process:

1. Create a project called fragments.
2. Under the Res/layout folder, create a new file called Fragment1.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" Fill_parent " 
  android: Background= "#00FF00" 
  android:orientation= "vertical" > 
 
  <textview 
    android:id= "@+id/lblfragment1" " 
    android:layout_width=" fill_parent " 
    android:layout_height=" wrap_content " 
    android:text=" This is Fragment #1 " 
    android:textcolor=" #000000 " 
    android:textsize=" 25sp "/> 
 
</LinearLayout> 

3. Under the Res/layout folder, create a new file called Fragment2.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" Fill_parent " 
  android: Background= "#FFFE00" 
  android:orientation= "vertical" > 
 
  <textview 
    android:layout_width= "Fill_" Parent " 
    android:layout_height=" wrap_content " 
    android:text=" This is fragment #2 " 
    android:textcolor=" # 000000 " 
    android:textsize=" "25sp"/> 
 
  <button 
    android:id= "@+id/btngettext" 
    android: Layout_width= "Wrap_content" 
    android:layout_height= "wrap_content" 
    android:onclick= "OnClick" 
    android:text= "Get-text in Fragment #1" 
    android:textcolor= "#000000"/> 
 
</LinearLayout> 

Code in the 4.main.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" fill_parent " 
  android:o" rientation= "Horizontal" > 
 
  <fragment 
    android:id= "@+id/fragment1" 
    Net.learn2develop.Fragments.Fragment1 " 
    android:layout_width=" 0px " 
    android:layout_height=" Match_parent " 
    android:layout_weight= "1"/> 
 
  <fragment 
    android:id= "@+id/fragment2" 
    Net.learn2develop.Fragments.Fragment2 " 
    android:layout_width=" 0px " 
    android:layout_height=" Match_ Parent " 
    android:layout_weight=" 1 "/> 
 
</LinearLayout> 

5. New two categories: Fragment1.java and Fragment2.java.
Code in the 6.fragment1.java.

Package net.learn2develop.Fragments; 
 
Import android.app.Activity; 
Import android.app.Fragment; 
Import Android.os.Bundle; 
Import Android.util.Log; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
 
public class Fragment1 extends Fragment { 
  @Override public 
  View Oncreateview (Layoutinflater inflater, ViewGroup container, 
      Bundle savedinstancestate) { 
 
    log.d ("Fragment 1", "Oncreateview"); 
 
    ---Inflate the layout for this fragment---return 
    inflater.inflate (R.layout.fragment1, container, false); 
} 

Code in the 7.fragment2.java.

Package net.learn2develop.Fragments; 
 
Import android.app.Fragment; 
Import Android.os.Bundle; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.Button; 
Import Android.widget.TextView; 
Import Android.widget.Toast; 
 
public class Fragment2 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); 
  } 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.