Android Click the left and right toggle page fragement

Source: Internet
Author: User

Do a simple click left and right Switch page, the next time you send a click after the bottom icon will also switch and the text will also change the color

Just a tutorial, so just write with eclipse, useless as

The code is as follows

Mainactivity.java

 Packagecn.wuxiaocheng.fragment;Importandroid.app.Activity;ImportAndroid.app.FragmentManager;Importandroid.app.FragmentTransaction;ImportAndroid.os.Bundle;ImportAndroid.view.Window;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsonclicklistener{PrivateFragmentmanager FM; Privatefragmenttransaction ft; //two pages    PrivateFragmenthome FH; PrivateFragmentset FS; //two X Bottom    PrivateTextView Tvhome; PrivateTextView Tvset; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title); //Get rid of the top title barSetcontentview (R.layout.activity_main); //Initialize bottom ofInitview (); //Set the default fragmentsetdefaultfragment (); }    //Set the default fragment    Private voidSetdefaultfragment () {FM=Getfragmentmanager (); FT=fm.begintransaction (); FH=NewFragmenthome ();        Ft.replace (R.id.main_view, FH);    Ft.commit (); }    //initializing space and declaring events    Private voidInitview () {tvhome=(TextView) Findviewbyid (r.id.id_home); Tvset=(TextView) Findviewbyid (R.id.id_set); Tvhome.setonclicklistener ( This); Tvset.setonclicklistener ( This); }    //Click events@Override Public voidOnClick (View v) {FM=Getfragmentmanager (); //Opening fragment thingsFT =fm.begintransaction (); Switch(V.getid ()) { CaseR.id.id_home:if(FH = =NULL) {FH=NewFragmenthome ();            } ft.replace (R.id.main_view, FH);  Break;  CaseR.id.id_set:if(fs = =NULL) {FS=NewFragmentset ();            } ft.replace (R.id.main_view, FS);  Break; }        //Ft.addtobackstack (); //Things to submitFt.commit (); }}

Fragmenthome.java

 Packagecn.wuxiaocheng.fragment;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classFragmenthomeextendsfragment{@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V Iew= Inflater.inflate (R.layout.fragment_home,NULL); returnview; }}

Fragmentset.java

 Packagecn.wuxiaocheng.fragment;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup; Public classFragmentsetextendsfragment{@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View V Iew= Inflater.inflate (R.layout.fragment_set,NULL); returnview; }}

The layout code is as follows:

Activity_main.xml

<LinearLayoutxmlns: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" >        <FramelayoutAndroid:id= "@+id/main_view"Android:layout_height= "0DP"Android:layout_width= "Match_parent"Android:layout_weight= "1"></Framelayout>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "50DP"android:orientation= "Horizontal"Android:background= "#eeeeee"        >                <TextViewAndroid:id= "@+id/id_home"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"android:gravity= "Center"Android:text= "Home" />        <TextViewAndroid:id= "@+id/id_set"Android:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"android:gravity= "Center"Android:text= "Settings" />                </LinearLayout></LinearLayout>

Fragment_home.xml

<?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:gravity= "Center" >        <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"android:textsize= "26SP"Android:text= "Home"/></LinearLayout>

Fragment_set.xml

<?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:background= "#00FF66"android:gravity= "Center" >        <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:textcolor= "#FF0033"android:textsize= "26SP"Android:text= "Set"/></LinearLayout>

Effects such as

Android Click the left and right toggle page fragement

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.