To achieve the fragment conversion, two fragment are required.
The first is the XML layout
<?xml version= "1.0" encoding= "Utf-8"?><LinearLayout xmlns: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:orientation= "Vertical"android:gravity= "Center"Tools:context= "Com.example.wdh.fragmentwork.MainActivity" > <LinearLayout Android:id= "@+id/show"Android:layout_width= "Match_parent"Android:layout_height= "261DP"android:orientation= "vertical" > </LinearLayout> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"android:gravity= "Center_horizontal" > <Button Android:id= "@+id/btnshow"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:onclick= "OnClick"Android:text= "@string/btnshow"/> </LinearLayout></LinearLayout>
View Code
And then the first fragment.
<?xml version= "1.0" encoding= "Utf-8"?><framelayoutxmlns: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 "tools:context=" Com.example.wdh.fragmentwork.FirstFragment "><TextView android:layout_width=" Match_parent " Android:layout_height= "Match_parent" android:gravity= "center" Style= "@style/first"/> </FrameLayout>
View Code
And then a second fragment.
<?xml version= "1.0" encoding= "Utf-8"?><framelayoutxmlns: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 "tools:context=" Com.example.wdh.fragmentwork.ScondFragment "><TextView android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:gravity= "center" Style = "@style/scond"/></framelayout>
View Code
At last
Packagecom.example.kimdemon.fragment;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;Importandroid.view.KeyEvent;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener{first Fragment1; Second Fragment2;Private BooleanQiehuan =true;Private BooleanTuichu =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Fragmentmanager Manager=Getfragmentmanager (); Fragmenttransaction Transaction=manager.begintransaction (); Fragment1=NewFirst (); Transaction.add (R.ID.YF_SHOW,FRAGMENT1); Transaction.commit ();} @Override Public voidOnClick (view view) {if(View.getid () = =r.id.yf_btn1) {Tuichu=true; if(Qiehuan) {Fragmentmanager Manager=Getfragmentmanager (); Fragmenttransaction Transaction=manager.begintransaction (); if(Fragment2 = =NULL) {Fragment2=NewFragment2 (); Transaction.replace (R.id.yf_show,fragment2); Transaction.commit (); Qiehuan=false; } Else{transaction.replace (r.id.yf_show,fragment2); Transaction.commit (); Qiehuan=false; } }Else{Toast.maketext ( This, "This is second fragment", Toast.length_short). Show (); } }} Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {if(Event.getkeycode () ==keyevent.keycode_back&&Tuichu) {Fragmentmanager Manager=Getfragmentmanager (); Fragmenttransaction Transaction=manager.begintransaction (); Qiehuan=true; Tuichu=false; Transaction.replace (R.ID.YF_SHOW,FRAGMENT1); Transaction.commit ();return false; } Else{finish (); } return Super. OnKeyDown (KeyCode, event);}}View Code
This homework feeling has been in the foggy, the whole person is in the state of being ignorant ...
END
Android jobs: Conversion of fragment