Android training-create Fragment

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/basics/fragments/creating.html

You can think of a fragment as a module area of an activity. It has its own lifecycle and receives its own input events, in addition, you can add and delete an activity when it is running (this is a bit like a child activity, you can reuse them in different activities ). This section describes how to use the supported class libraries to expand fragment so that your applications can be compatible with older versions like android1.6.

Note: if, for some reason, you decide that your application requires an API level above 11, you do not need to use the support class library, the built-in fragment class and related APIs can be used instead. Note that the focus of this lesson is to use APIs that support class libraries. It uses a special package signature, in addition, the API name may be slightly different from the version included in the platform.

 

Create a fragment class

To create a fragment, you must inherit the fragment class, rewrite the key lifecycle method, and insert your application logic into it, which is similar to the activity class.

One difference during fragment creation is that you must use the oncreateview () callback to define the layout. In fact, to get a running fragment, this is only the only callback method you need. For example, the following is a simple fragment with its own layout specified:

Import Android. OS. Bundle;

Import Android. Support. v4.app. Fragment;

Import Android. View. layoutinflater;

Import Android. View. viewgroup;

 

Public class articlefragment extends fragment {


@ Override


Public View oncreateview (layoutinflater Inflater, viewgroup container,


Bundle savedinstancestate ){


// Inflate the layout for this fragment


Return Inflater. Inflate (R. layout. article_view, container, false );


}

}

 

Like an activity, fragment should implement other lifecycle callback methods, allowing you to manage its status (add or delete) in the activity ), and the status of the activity during transition between its lifecycle states. For example, when the onpause () method of an activity is called, any fragment in the activity will receive a call to the onpause () method.

For the fragment lifecycle and valid callback methods, see the fragment Development Guide.

Add a fragment to an activity using XML

Fragment is a reusable and modular UI component. Each fragment class instance must be associated with an activity whose parent class is fragmentactivity. You can complete this association by defining each fragment in your activity layout XML file.

Note: fragmentactivity is a special activity that supports class libraries. It is used to process fragment earlier than API level 11. If your system version is at least API level 11 or higher, you can use a regular activity.

When the screen is considered large enough, the layout file in the next example adds two fragment to an activity (the file is placed in the directory name specified by large ).

res/layout-large/news_articles.xml:

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"


Android: Orientation = "horizontal"


Android: layout_width = "fill_parent"


Android: layout_height = "fill_parent">

 


<Fragment Android: Name = "com. example. Android. fragments. headlinesfragment"


Android: Id = "@ + ID/headlines_fragment"


Android: layout_weight = "1"


Android: layout_width = "0dp"


Android: layout_height = "match_parent"/>

 


<Fragment Android: Name = "com. example. Android. fragments. articlefragment"


Android: Id = "@ + ID/article_fragment"


Android: layout_weight = "2"


Android: layout_width = "0dp"


Android: layout_height = "match_parent"/>

 

</Linearlayout>

 

Tip: For information on creating la s for different screen sizes, see support for different screen sizes.

The following is the activity that uses the layout:

Import Android. OS. Bundle;

Import Android. Support. v4.app. fragmentactivity;

 

Public class mainactivity extends fragmentactivity {


@ Override


Public void oncreate (bundle savedinstancestate ){


Super. oncreate (savedinstancestate );

Setcontentview (R. layout. news_articles );


}

}

Note:When you add a fragment to the activity layout by defining the fragment in the layout XML file, you cannot delete the fragment at runtime. If you want to switch the fragment during user interaction, you must add the fragment to the activity when the activity is started for the first time.

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.