Android jobs: Conversion of fragment

Source: Internet
Author: User

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

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.