Andriod fragment-Add a fragment to the activity

Source: Internet
Author: User

In general, fragment is part of the overall view Hierarchy Tree of the activity and is embedded into a part of the interface of the host activity. There are two ways to add fragment to the activity layout:

1. Declare fragment in the activity layout file.

You can specify layout attributes for fragment like a view. The following example specifies two fragment layout files for the activity.

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "horizontal"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Fragment Android: Name = "com. example. News. articlelistfragment"
Android: Id = "@ + ID/List"
Android: layout_weight = "1"
Android: layout_width = "0dp"
Android: layout_height = "match_parent"/>
<Fragment Android: Name = "com. example. News. articlereaderfragment"
Android: Id = "@ + ID/Viewer"
Android: layout_weight = "2"
Android: layout_width = "0dp"
Android: layout_height = "match_parent"/>
</Linearlayout>

The Android: Name attribute in the <fragment> element specifies the fragment to be instantiated in the layout.

When the system creates the activity layout, it instantiates each fragment specified in the layout and calls oncreateview () to obtain the layout of each fragment. Then, the system inserts a view directly returned through the declaration in the <fragment> element in the activity layout.

Note: Each fragment requires a unique identifier, so that the system can use this ID to restore the fragment when the activity is restarted (and you can use this ID to obtain the fragment for executing the transaction, such as delete ). There are three methods to provide ID for fragment:

A. Use the Android: Id attribute to set a unique ID;

B. Use the Android: Tag attribute to set a unique string;

C. If the first two attributes are not set, the ID of the container view is used.

2. Program to add fragment to an existing viewgroup.

You can add fragment to the activity layout at any time when the activity is running. You only need to specify a viewgroup for fragment. To use a fragment transaction (such as adding, deleting, or replacing fragment) in an activity, you must use an APIS from fragmenttransaction. You can obtain a fragmenttransaction instance from the activity as follows:

Fragmentmanager =
Getfragmentmanager ()
Fragmenttransaction = fragmentmanager. begintransaction ();

Then, you can use the add () method to add fragment to the specified view, for example:

Examplefragment fragment = new examplefragment ();
Fragmenttransaction. Add (R. Id. fragment_container, fragment );
Fragmenttransaction. Commit ();

The first parameter passed to the add () method is the viewgroup to which fragment should be placed. The viewgroup is specified by the resource ID, and the second parameter is the fragment to be added.

Once the fragmenttransaction object changes, you must call the commit method to submit the change impact.

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.