Android Project Learning essay Three (Fragment)

Source: Internet
Author: User

1. Define framelayout in the layout file of the main page (activity reference fragment), load fragment

<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/fl_content"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

</FrameLayout>

2. Initialize Fragment

private void Initfragment () {
Fragment Manager
Fragmentmanager fm = Getsupportfragmentmanager ();
Fragmenttransaction transaction = Fm.begintransaction ();//Start transaction
Replace the frame layout with the corresponding fragment
Transaction
. replace (R.id.fl_content, New Contentfragment (), tag_content);
Transaction.replace (R.id.fl_left_menu, New Leftmenufragment (),
Tag_left_menu);
Transaction.commit ();//Commit a transaction
Fm.findfragmentbytag (tag_content);
}

3. Define fragment base class

 Public Abstract classBasefragmentextendsFragment { PublicActivity mactivity; //Fragment was created@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Mactivity= Getactivity ();//Gets the activity object that is located    }    //initializing the Fragment layout@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V Iew=Initview (); returnview; }    //Activity creation End@Override Public voidonactivitycreated (Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate);    InitData (); }    /*** Initialize layout, subclasses must implement*/     Public AbstractView Initview (); /*** Initialize data, subclass can not implement*/     Public voidInitData () {}}
View Code

4. Define fragment base class

Android Project Learning essay Three (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.