Fragments was introduced in Android 3.0 (API level 11), but it is backward compatible. It supports the old Android version.
Only need to import jar package support (in this directory: Android-SDK-Windows \ extras \ Android \ support \ V4 \ android-support-v4.jar ),
Mainly used to implement the following UI Layout
To implement such an activity with multiple complex view la S, you can use viewgroup or customize some la s according to the previous conventions.
Fragments meets our needs. It is more powerful than our original implementation method,
In short, I personally think fragments can be considered as a view, what makes him more powerful than view is that he has a lifecycle and the lifecycle will change as the activity he attaches changes.
Oncreate ()
The system CILS this when creating the fragment. Within your implementation, you shoshould initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
Oncreateview ()
The system callthis when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must returnView
From this method that is the root of your fragment's layout. You can return NULL if the fragment does not provide a UI.
Onpause ()
The system callthis method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed ). this is usually where you shocould commit any changes that shocould be persisted beyond the current user session (because the user might not come back ).
This is his three main methods, or the lifecycle. when his parent activity onpause ();, he will also call onpause (); his parent activityonresume (); he calls onresume ();
This is very flexible. For example, if you have a large number of Bitmap views, you can release useless resources in onpause () and load them in onresume.
These fragments are managed by a stack-like container such as fragmentmanager.Findfragmentbyid ()
OrFindfragmentbytag ()
Find the fragments added to the page.
Here is a small demo:
First, we need to write a class that inherits fragment. You can regard it as a module in your UI.
Public Class Examplefragment Extends Fragment {
@ Override
Public View oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate ){
System. Out. println ("oncreateview ");
// Inflate the layout for this fragment
Return Inflater. Inflate (R. layout. example_fragment, container, False );
}
@ Override
Public Void Oncreate (bundle savedinstancestate ){
System. Out. println ("oncreate ");
Super . Oncreate (savedinstancestate );
}
@ Override
Public Void Onpause (){
System. Out. println ("onpause ");
Super . Onpause ();
}
@ Override
Public Void Onresume (){
// Todo auto-generated method stub
Super . Onresume ();
System. Out. println ("onresume ");
}
}
Based on the print output, you will find that oncreateview is called only at the first startup, and this is the layout of your UI module.
Next is the activity you want to display:
Public Class Myfragmentactivity Extends Fragmentactivity {
@ Override
Protected Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Examplefragment = new examplefragment ();
// Fragmentmanager = getsupportfragmentmanager ();
// Fragmenttransaction = fragmentmanager. begintransaction ();
// Fragmenttransaction. Add (R. Id. Viewer, examplefragment );
// Fragmenttransaction. Commit ();
}
}
Then the activity layout is very simple.
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Match_parent"
Android: layout_height = "Match_parent"
Android: Orientation = "Horizontal" >
< Fragment
Android: ID = "@ + ID/List"
Android: Name = "Com. Fragment. Demo. examplefragment"
Android: layout_width = "0dp"
Android: layout_height = "Match_parent"
Android: layout_weight = "1" />
< Fragment
Android: ID = "@ + ID/Viewer"
Android: Name = "Com. Fragment. Demo. examplefragment"
Android: layout_width = "0dp"
Android: layout_height = "Match_parent"
Android: layout_weight = "2" />
</ Linearlayout >
This is done. Two fragment instances are generated. This is a static addition.
If it is a dynamic addition, you need to useCode:
Examplefragment =NewExamplefragment ();
Fragmentmanager = getsupportfragmentmanager ();
Fragmenttransaction = fragmentmanager. begintransaction ();
Fragmenttransaction. Add (R. Id. Viewer, examplefragment );
Fragmenttransaction. Commit ();
This is almost the first knowledge. I hope someone can add that the next project will have a chance to use it.