Android Fragment details 3: Fragment implementation Interface

Source: Internet
Author: User

Add User Interface for fragment
Fragment is generally part of the user interface of an activity. It embeds its own layout into the layout of the activity. 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 layout of fragment.

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 layoutxml. To help you do this, onCreateView () provides a LayoutInflater object.

Example: The following code shows how a Fragment subclass generates an object from the example_fragment.xml file of layoutxml.

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 stores the layout of fragment. The savedInstanceState parameter is a Bundle, which is similar to the Bundle in onCreate () of the activity and used for State recovery. However, the onCreate () of fragment also has the Bundle parameter. Therefore, the data stored in the Bundle here is different from the data stored in onCreate. For more information, see "control fragment lifecycle.

The Inflate () method has three parameters:

Resource ID of 1layout.

2. ViewGroup that stores layout of fragment.

3. boolean data indicates whether to append layout to the container during the layout of fragment (in this example, because the system has inserted layout into the container, so the value is false. If it is true, it will be redirected to creating a redundant ViewGroup in the final layout (I cannot understand this sentence, but I should be correct in translation )).

Now you can see how to create layout for fragment. The following describes how to add it to activity.

Add fragment to activity
In general, fragment combines its layout into the activity as part of activitiy's loyout. There are two ways to add a fragment to the activity:

Method 1: declare fragment in the layoutxml file of the activity
The following code indicates that an activity contains two fragment items:

<? 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>

<Fragment> declare a fragment.

When the system creates layout in the preceding example, it instantiates each fragment and then calls their onCreateView () method to obtain the layout of each fragment. The system inserts the view object returned by fragment into the position of the <fragment> element, directly replacing the <fragment> element.

Note: Each fragment must provide an ID. When the activity is re-created, the system uses it to restore fragment. You can also use it to operate fragment for other things, such as deleting it. There are three methods to provide an ID for fragment:

1. assign a number to the android: id attribute.

2. assign a string to the android: tag attribute.

3. If you do not use any of the above methods, the system will use the ID of the fragment container.

Method 2: Add fragment to a ViewGroup in the code
This method can be used to add fragment to layout of the activity at runtime. You only need to specify a ViewGroup containing fragment.

To complete fragment transactions (such as adding, deleting, and replacing), you must use the FragmentTransaction method. You can get the FragmentTransaction from the activity, as follows:

FragmentManagerfragmentManager = getFragmentManager ()
FragmentTransactionfragmentTransaction = fragmentManager. beginTransaction ();

Then you can use the add () method to add a fragment. It has a parameter used to specify the ViewGroup that can hold 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 parameter is the fragment to be added. Once you change the fragment through FragmentTransaction, you must call the commit () method to submit these changes.

Not only in the unbounded fragment, but also in the fragment with an interface, you can use tags as a flag. In this way, you need to call findFragmentTag () when obtaining the fragment object ().

Add a fragment with no interface
The above demonstrates 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 interface, you need to call the add (Fragment, String) method in the activity (it supports using a unique String as the "tag" of fragment, rather than viewID ). The added fragment does not have an interface, so you do not need to call the onCreateView () method when implementing it.

Using a tag string to identify a fragment is not only used for fragment without interfaces. You can also use it for fragment with Interfaces. However, if a fragment has no interfaces, the tag string will be its only choice. To obtain the fragment identified by a tag, use findFragmentByTab ().

 

From the column of nkmnkm

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.