Android Training-Start-create Fragment using Fragment construct dynamic ui-

Source: Internet
Author: User

Create fragment last Lesson next lesson this lesson teaches you
    1. Create a Fragment class
    2. Adding Fragment to Activity using XML
You also need to read
    • Fragments
Try the Download sample

Fragmentbasics.zip

You can think of fragment as a modular part of activity that has its own life cycle, receiving its own input events, and you can add it to activity or remove it from activity while the activity is running (a bit like the one you can do in different act The "Child activity" used in ivity). This lesson teaches you how to use the Support Library extension Fragment class to keep your app compatible with devices running down to Android version 1.6.

Note: If you decide that your app requires a minimum API level of 11 or higher, you don't need to use a support library, you can use Fragment a framework built by classes and related APIs. You need to note that this lesson focuses on APIs that use a support library, which uses specific package signatures, and sometimes the API names and platforms contain different versions.

Before starting this class, you must set up your Android project to use the support library. If you have not used the support library before, set up your project using the V4 Library According to the Settings support Library document. However, you can also use the v7 AppCompat Library, which is compatible with Android 2.1 (API Level 7) and contains Fragment APIs that allow your activity to include the action Bar.

Create a Fragment class

In order to create a fragment, inherit the Fragment class, and then override the critical life cycle method to insert your application logic, and you Activity do similar to the class.

A different place is when you create one Fragment you must use the onCreateView() callback method to define the layout. In fact, this is to get fragment to run the only callback method you need to implement. For example, here is a simple fragment that specifies its own layout:

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) {        //For this fragment render layout        return Inflater.Inflate(R.Layout.Article_view, Container, false);    }}

Like an activity, a fragment should implement other life-cycle callback methods, which allow you to manage the state of fragment when fragment is added to the activity or removed from activity or when the activity's life state transitions. For example, when an activity onPause() method is called, any fragment in the activity receive a onPause() call.

For more information on the fragment life cycle and callback methods, see the Fragments Developer's Guide.

Adding Fragment to Activity using XML

Fragment is a reusable, modular UI component, Fragment and each instance of a class must be associated with a parent FragmentActivity . You can implement this association by defining each fragment in your activity's layout XML file.

Note: FragmentActivity A special activity provided by the library in order to process fragment on a system version older than API level 11. If the minimum system version you support is API level 11 or higher, you can use a regular one Activity .

Here is an large example of a layout file that adds two fragment to an activity when the device is a large screen (specified by a qualifier in the directory name).

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 more information on creating layouts for different screen sizes, see support for different screen sizes.

Then use the layout for your activity:

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);    }}

If you use the V7 AppCompat Library, your activity should inherit ActionBarActivity , it is FragmentActivity a subclass (for more information, see Add Action Bar).

Note: When you add fragment to activity by defining fragment in the layout XML, you cannot delete this fragment at run time. If you plan to swap in or out of fragment based on user action, you must add fragment to activity the first time the activity is started, which is described in the next lesson.

Last lesson Next lesson

Android Training-Start-create Fragment using Fragment construct dynamic ui-

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.