To add a user interface for fragment:
Fragment is generally used as part of the activity's user interface to embed its own layout in the activity's layout. One
To provide layout for fragment, you must implement the Oncreateview () callback method, and then return a view object in this method, which is the root of the fragment layout.
Note: If your fragment is derived from listfragment, you do not need to implement the Oncreateview () method because the default implementation has returned the ListView control object for you.
To return the layout object from the Oncreateview () method, you can generate the layout object from the Layoutxml. To help you do this, Oncreateview () provides a Layoutinflater object.
Example: The following code shows how a subclass of fragment can generate an object from the Layoutxml file example_fragment.xml.
- publicstaticclassexamplefragmentextendsfragment{
- @Override
- Publicviewoncreateview (layoutinflaterinflater,viewgroupcontainer,bundlesavedinstancestate) {
- //inflate The layout for this fragment
- Returninflater.inflate (R.layout.example_fragment,container,false);
- }
- }
The container in the Oncreateview () parameter is the ViewGroup object that holds the layout of the fragment. The Savedinstancestate parameter is a bundle that is similar to the bundle in the activity's OnCreate () for state recovery. But fragment's OnCreate () also has bundle parameters, so the data stored in the bundle here is different from the data stored in the OnCreate (). For more information, refer to the "manipulating the Fragment Life cycle" section.
The inflate () method has three parameters:
The resource ID of the 1layout.
2 Store the layout of the fragment ViewGroup.
3 Boolean data indicates whether layout is appended to the container during the creation of the fragment layout (in this case, because the system has already inserted layout into the container, the value is false, If true leads to the creation of redundant ViewGroup in the final layout (which I do not understand, but I should be correct)).
Now that you see how to create layout for fragment, here's how to add it to your activity.
Add fragment to Activity
In general, fragment merges its layout into the activity as part of the activitiy loyout, and there are two ways to add a fragment to the activity:
Method One: Declare fragment in the activity's Layoutxml file
In the following code, an activity contains two fragment:
- <?xmlversion= "1.0" encoding="Utf-8"?>
- <linearlayoutxmlns:android="Http://schemas.android.com/apk/res/android"
- android:orientation="Horizontal"
- Android:layout_width="Match_parent"
- android:layout_height="Match_parent" >
- <fragmentandroid:name="Com.example.news.ArticleListFragment"
- android:id="@+id/list"
- android:layout_weight="1"
- Android:layout_width="0DP"
- android:layout_height="Match_parent"/>
- <fragmentandroid:name="Com.example.news.ArticleReaderFragment"
- android:id="@+id/viewer"
- android:layout_weight="2"
- Android:layout_width="0DP"
- android:layout_height="Match_parent"/>
- </LinearLayout>
A fragment is declared in <fragment>.
When the system creates layout in the previous example, it instantiates each fragment and then calls their Oncreateview () method to get the layout of each fragment. The system inserts the View object returned by the fragment into the position of the <fragment> element, replacing the <fragment> element directly.
Note: Each fragment needs to provide an ID that the system uses to restore fragment when the activity is recreated, and you can use it to manipulate fragment to do other things, such as deleting it. There are three ways to give fragment an ID:
1 assigns a specific identifier to the Android:id property.
2 assigns a tag name to the Android:tag property.
3 If you do not use either of these methods, the system will use the ID of the fragment container.
Method Two: Add fragment to a viewgroup in the code
This method can be used to add fragment to the activity's layout at run time. You only need to specify a viewgroup to contain the fragment.
In order to complete fragment transactions (such as Add, delete, replace, etc.), you must use the Fragmenttransaction method. You can get the fragmenttransaction from the activity as follows :
- Fragmentmanagerfragmentmanager =getfragmentmanager ()
- Fragmenttransactionfragmenttransaction =fragmentmanager.begintransaction ();
You can then add a fragment with the Add () method, which has parameters that specify the ViewGroup that holds the fragment. As follows:
- Examplefragmentfragment =newexamplefragment ();
- Fragmenttransaction.add (r.id.fragment_container,fragment);
- Fragmenttransaction.commit ();
The first parameter of add () is the container viewgroup, and the second is the fragment to be added. Once you have made changes to fragment through fragmenttransaction, you must invoke the method commit () to commit the changes.
Tag can also be used as a flag in fragment with interface, not only in fragment without interface, so Findfragmenttag () is called when the fragment object needs to be acquired.
Add a fragment with no interface
The above shows how to add fragment to provide the interface, however, you can also use fragment to provide background behavior for the activity without displaying the fragment interface.
To add a Fragment without an interface, call method Add (fragment,string) in the activity (it supports using a unique string as the Fragment "tag" instead of the Viewid). This adds fragment because there is no interface, so you do not need to call the implement Oncreateview () method when implementing it.
Using the tag string to identify a fragment is not used only on fragment with no interface, you can also use it on an interface fragment, but if a fragment does not have an interface, the tag string becomes its only option. Gets the fragment identified by tag, using Method Findfragmentbytab ().
Copy to Google TranslateTranslation Results
Android Fragment (iii): Implementation of the Fragment interface