Android Fragment (iii): Implementation of the Fragment interface

Source: Internet
Author: User

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.

    1. publicstaticclassexamplefragmentextendsfragment{
    2. @Override
    3. Publicviewoncreateview (layoutinflaterinflater,viewgroupcontainer,bundlesavedinstancestate) {
    4. //inflate The layout for this fragment
    5. Returninflater.inflate (R.layout.example_fragment,container,false);
    6. }
    7. }

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:

  1. <?xmlversion= "1.0" encoding="Utf-8"?>
  2. <linearlayoutxmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:orientation="Horizontal"
  4. Android:layout_width="Match_parent"
  5. android:layout_height="Match_parent" >
  6. <fragmentandroid:name="Com.example.news.ArticleListFragment"
  7. android:id="@+id/list"
  8. android:layout_weight="1"
  9. Android:layout_width="0DP"
  10. android:layout_height="Match_parent"/>
  11. <fragmentandroid:name="Com.example.news.ArticleReaderFragment"
  12. android:id="@+id/viewer"
  13. android:layout_weight="2"
  14. Android:layout_width="0DP"
  15. android:layout_height="Match_parent"/>
  16. </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 :

    1. Fragmentmanagerfragmentmanager =getfragmentmanager ()
    2. 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:

    1. Examplefragmentfragment =newexamplefragment ();
    2. Fragmenttransaction.add (r.id.fragment_container,fragment);
    3. 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

Related Article

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.