How to use Android fragment

Source: Internet
Author: User
Tags unique id


Normally fragment as part of the host activity UI is embedded as part of the activity's entire view hierarchy.
There are 2 ways you can add a fragment to activity layout:

I. Declare fragment in the activity's layout file
You can specify the Layout property (sdk3.0 later) for fragment as you would for a view.
The example is an activity with 2 fragment:

<?XML version= "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>

The Android:name property in

    <fragment> Specifies the fragment class that is instantiated in layout.
When the system creates this activity layout, it instantiates each fragment specified in the layout and invokes each of the Oncreateview () methods on it to get the layout of each fragment. The
     system will insert the View returned from fragment directly into the <fragment> element's location.
Note: Each fragment requires a unique identity, and if the activity restarts, the system can be used to recover the fragment (and you can also use it to capture fragment to process transactions, such as removing it.)  
There are 3 ways to provide an identity for a fragment:
     provides a unique ID for the Android:id property.
     provides a unique string for the Android:tag property.
     If none of the above 2 are provided, the system uses the ID of the container view.
    
Second, use Fragmentmanager to add fragment to an existing viewgroup.
     Fragment can be added to activity layout at any time when the activity is running. Simply specify a viewgroup that needs to be placed fragment.
     To manipulate fragment transactions in your activity (for example, add, remove, or replace a fragment), you must use an API from Fragmenttransaction. The
can obtain an instance of fragmenttransaction from your activity as follows:
   

Fragmentmanager Fragmentmanager == fragmentmanager.begintransaction ();

You can then use the Add () method to add a fragment, specify the fragment to add, and the view to insert.

New examplefragment (); Fragmenttransaction.add (R.id.fragment_container, fragment); Fragmenttransaction.commit ();

The first parameter of

    Add () is the viewgroup that fragment to put, specified by the resource ID, and the second parameter is the fragment that needs to be added.
     Once changed with Fragmenttransaction, commit () must be called in order for the change to take effect.
    
Administration fragment
   
   Managing fragment in your activity you need to use a class called Fragmentmanager, by calling the Getfragmentmanager () method to instantiate the management class in your activity species. Some of the main methods of the
   Fragmentmanager class are Findfragmentbyid () to get an activity about the fragment layout.
   Of course there are similar findfragmentbytag () methods, as well as when Popbackstack () fragment out of the stack can register Addonbackstackchangedlistener () Management.
   Concrete can be understood in the Android.app.FragmentManager class

Optimize fragment things handling

     A good feature in adding, deleting, replacing fragment in activity can use the Fragmenttransaction class to improve the efficiency of batch processing, which is similar to SQLite's database update principle.       

Fragmentmanager Fragmentmanager = Getfragmentmanager (); // instantiate the Fragmentmanager class Fragmenttransaction transaction = fragmentmanager.begintransaction (); // get an instance of a thing processing by the BeginTransaction method. 

You can use Add (), remove (), and replace () during this time. Commit () can be executed when the change is finally needed.

transaction.replace (R.id.fragment_container, newfragment); Transaction.addtobackstack (null  ); Transaction.commit ();        

For situations where you need to keep the fragment state when switching fragment, such as the bottom tab toggle mode, you can use Hide () to cache fragment and show () that need to be hidden to show hidden fragment. Of course fragment is null when you add () first

Fragment and activity communicate with each other

Usually fragment we put in the usual standard controls or custom controls, basically the same as the activity, but how the view layout in the fragment is also put into the activity,
Here eoeandroid suggest that there are 3 ways to achieve:

View ListView = getactivity (). Findviewbyid (R.ID.CWJ);
The Getactivity method can be used to obtain an activity in the fragment, where the CWJ is a fragment, in the activity of the layout as follows

<?XML version= "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=". Articlelistfragment "Android:id= "@+id/cwj"Android:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Match_parent" />                <FragmentAndroid:name=". Articlereaderfragment "Android:id= "@+id/smart"Android:layout_weight= "2"Android:layout_width= "0DP"Android:layout_height= "Match_parent" />    </LinearLayout>

The second obtains the instance through the Getfragmentmanager method,
Examplefragment fragment = (examplefragment) Getfragmentmanager (). Findfragmentbyid (R.ID.CWJ);

There is also a common way to define interfaces to communicate in a way that API documents recommend this way











How to use Android fragment

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.