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://"Xmlns:tools= "Http://"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.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://"Xmlns:tools= "Http://"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 (; 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.