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 write with Eclipse, no use as

The code is as follows

Mainactivity.java

package cn.wuxiaocheng.fragment;import android.app.activity;import android.app.fragmentmanager; import android.app.fragmenttransaction;import android.os.bundle;import android.view.window; import android.view.view;import android.view.view.onclicklistener;import  android.widget.textview;public class mainactivity extends activity implements  Onclicklistener{    private fragmentmanager fm;    private  fragmenttransaction ft;        //of two pages      private FragmentHome fh;    private FragmentSet fs;         //two bottom     private TextView tvhome;     private TextView tvset;         @Override     protected void onCreate (bundle savedinstancestate)  {        super.oncreate ( Savedinstancestate);         requestwindowfeature (Window.FEATURE_NO_ Title)         //Remove top title bar          setcontentview (R.layout.activity_main);                     //Initialize Bottom          initview ();         //Set the default fragment         setdefaultfragment ();     }    //Set the default fragment     private void setdefaultfragment ()  {         fm = getfragmentmanager ();        ft =  Fm.begintransaction ();  &nbSp;      fh = new fragmenthome ();         ft.replace (R.ID.MAIN_VIEW, FH);         Ft.commit ();     }    //initialization space and declaring events     private  Void initview ()  {        tvhome =  (TextView)   Findviewbyid (r.id.id_home);        tvset =  (TextView)   Findviewbyid (R.id.id_set);                 tvhome.setonclicklistener (This);         Tvset.setonclicklistener (this);     }    //Click events      @Override     public void onclick (view v)  {         fm = getfRagmentmanager ()         //Open fragment things          ft = fm.begintransaction ();                 switch  (V.getid ())  {         case r.id.id_home:            if   (Fh == null)  {                 fh = new fragmenthome ();             }             Ft.replace (R.ID.MAIN_VIEW, FH);             break;        case r.id.id_set:             if  (fs == null)  {                 fs = new fragmentset ();             }             Ft.replace (R.ID.MAIN_VIEW, FS);             break;        }        //  ft.addtobackstack ()          //     Things Submitted     ft.commit ();     }}

Fragmenthome.java

Package Cn.wuxiaocheng.fragment;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class FragmentHome  Extends fragment{@Override public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle                Savedinstancestate) {View view = Inflater.inflate (R.layout.fragment_home,null);    return view; }}

Fragmentset.java

Package Cn.wuxiaocheng.fragment;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class FragmentSet extends fragment{@Override public View oncreateview (layoutinflater inflater, ViewGroup container, Bundle Savedin                Stancestate) {View view = Inflater.inflate (R.layout.fragment_set,null);    return view; }}

The layout code is as follows:

Activity_main.xml

<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"  >        <FrameLayout              android:id= "@+id/main_view"              android:layout_height= "0DP"              android:layout_width= "Match_parent"              android:layout_weight= "1" ></FrameLayout>         <linearlayout         android:layout_width= " Match_parent "        android:layout_height= "50DP"          android:orientation= "Horizontal"         android:background= "#eeeeee"         >                 <TextView             android:id= "@+id/id_home"              android:layout_width= "0DP"              android:layout_height= "Match_parent"              android:layout_weight= "1"             android: gravity= "center"             android:text= "Home"  />     &Nbsp;  <textview            android: 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"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/        Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:gravity=" Center "> <textview android:layout_height= "wrap_content" android:layout_width= "Wrap_content" android:t Extsize= "26sp" android:text= "Home"/></linearlayout>

Fragment_set.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background=" #00FF66 " android:gravity= "center" > <textview android:layout_height= "wrap_content" android:layout_width = "Wrap_content" android:textcolor= "#FF0033" android:textsize= "26sp" android:text= "Set"/></linea Rlayout>

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.