The use of Android fragment

Source: Internet
Author: User

The fragment in Android is similar to an IFRAME in HTML, where the right side of the display can be controlled by the left button.

Project Catalogue

You need to define three fragment classes, each of which displays different content, each fragment a layout file

Layout file

Define buttons and frame layouts in the main layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal">    <LinearLayoutAndroid:id= "@+id/left"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"android:orientation= "vertical">        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "F1"Android:onclick= "Click1"/>        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "F2"Android:onclick= "Click2"/>        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "F3"Android:onclick= "Click3"/>    </LinearLayout>    <FramelayoutAndroid:id= "@+id/fragment"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    </Framelayout></LinearLayout>

Frame Layout File

 <?  xml version= "1.0" encoding= "Utf-8"  ?>  <  linearlayout  xmlns:android  = "Http://schemas.android.com/apk/res/android"   Android:orientation  = "Vertical"   Android:layout_width  = "Match_parent"   Android:layout_height  = "Match_parent"   Android:background  = "#ff0000"  >  </ linearlayout  >  
Fragment1 class

The Fragment1 class needs to inherit the fragment, which is noted here is the fragment in the Adroind class library, not the Adroind class in the Support class library.

 PackageXidian.dy.com.chujia;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;/*** Created by Dy on 2016/8/25.*/ Public classFragment1extendsfragment{@Nullable @Override//load our frame layout file     PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { View v= Inflater.inflate (R.layout.fragment1,NULL); returnv; }}
Activity

Define the frame layout in activity and switch frames when the Click event occurs

 PackageXidian.dy.com.chujia;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View; Public classMainactivityextendsappcompatactivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main); }     Public voidClick1 (View v) {Fragment1 F1=NewFragment1 (); Fragmentmanager FM=Getfragmentmanager (); Fragmenttransaction ft=fm.begintransaction ();        Ft.replace (r.id.fragment, F1);    Ft.commit (); }     Public voidClick2 (View v) {Fragment2 F2=NewFragment2 (); Fragmentmanager FM=Getfragmentmanager (); Fragmenttransaction ft=fm.begintransaction ();        Ft.replace (R.id.fragment, F2);    Ft.commit (); }     Public voidClick3 (View v) {Fragment3 F3=NewFragment3 (); Fragmentmanager FM=Getfragmentmanager (); Fragmenttransaction ft=fm.begintransaction ();        Ft.replace (R.id.fragment, F3);    Ft.commit (); }}

The use of 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.