Fragment usage (similar to. NET user controls, good reusability) .... ---------------Android

Source: Internet
Author: User

Just like the user control in WinForm, you can add a user control directly to the specified location when a page is initialized, or it can be added dynamically with more data.

For Android fragment, there are two ways, one called static loading, and one called dynamic loading.

Whether it's static or dynamic, there's a fragment activity first.

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context= "Com.example.administrator.myapplication.fragment"><TextViewAndroid:text= "Hello"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/sk"/></Relativelayout>
 Packagecom.example.administrator.myapplication;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; Public classFragmentextendsFragment {//must inherit this class@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return Super. Oncreateview (Inflater, container, savedinstancestate); }}

Then build another activity to show the static and dynamic methods of loading fragment

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context= "Com.example.administrator.myapplication.Main2Activity"    >    <FragmentAndroid:id= "@+id/fragment"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:name= "Com.example.administrator.myapplication.fragment"Android:tag= "Here is the static load, the above parameter name, is the class of fragment">    </Fragment>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/frame"android:orientation= "Horizontal"Android:tag= "Here is the dynamic loading, only need to have an ID, backstage according to this ID, in this layout inside load a fragment">    </LinearLayout></LinearLayout>

In the background code of this page, the following shows the static loading and dynamic loading

 protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_fragment);//This method must have, although the fill is not the current page, then the loaded page is the current page//The following is a static load, which allows access to the controls inside the fragment when statically loadedTextView textview=(TextView) Findviewbyid (r.id.sk); Textview.settext (Hello); //a detailed step-up of dynamic downloadFragment fragment =NewFragment ();//instantiate this fragment class.Fragmentmanager Fragmentmanager =Getfragmentmanager (); Fragmenttransaction Tran=fragmentmanager.begintransaction ();//Get a transactionTran.add (Fragment,NULL);//Add the contents of this transactionTran.commit ();//Commit a transaction}

Fragment usage (similar to. NET user controls, good reusability) .... ---------------Android

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.