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