Android-based pedometer (pedometer) explanation (a)--fragment page jump

Source: Internet
Author: User

The first thing to do is to implement a few page jumps, both similar to the click button to toggle the page.

Words do not say, this is a simple page switch demo, first put a few pieces of the implementation of the effect:

The bottom of the picture is 5 RadioButton, which is used for switching between pages. The rest is a linearlayout layout that is used to set up 5 fragment (fragments) of the page.

Altogether 5 classes are used: mainactivity, Myadapter, Fragmenthistory, Fragmentanalysis, Fragmentpedometer, FRAGMENTPK, FragmentSet

The main code for mainactivity is as follows:

Package Com.example.changepage1;import Java.util.arraylist;import Java.util.list;import android.os.Bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.widget.RadioGroup; public class Mainactivity extends Fragmentactivity {private Radiogroup rg;private list<fragment> fragments;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.page_mian); rg = (Radiogroup) Findviewbyid (r.id.radiogroup);//Instantiate radiogroupfragments = new arraylist< Fragment> ();//Add 5 Fragmentfragments.add respectively (new Fragmenthistory ()); Fragments.add (new Fragmentanalysis ()); Fragments.add (New Fragmentpedometer ()); Fragments.add (new FRAGMENTPK ()); Fragments.add (new Fragmentset ()); new Myadapter (This, fragments, r.id.fragment, RG);//Set Adapter}}

the code for Myadapter is as follows:

Package Com.example.changepage1;import Java.util.list;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmenttransaction;import Android.widget.radiogroup;import android.widget.radiogroup.oncheckedchangelistener;/** * This is an adapter written by yourself * * @author Li Wu Super Date:2015-1-2 time: 3 o'clock in the afternoon */public class Myadapter implements Oncheckedchangelistener {private list<fragment> FRAGM Ents; A tab page corresponds to a fragmentprivate radiogroup RGS; For switching tabprivate fragmentactivity fragmentactivity; Fragment belongs to activityprivate int fragmentcontentid; idprivate int currenttab = 2 of the area to be replaced in the activity; Current tab page index public myadapter (fragmentactivity fragmentactivity,list<fragment> fragments, int fragmentcontentid , Radiogroup rgs) {this.fragments = Fragments;this.rgs = Rgs;this.fragmentactivity = fragmentactivity; This.fragmentcontentid = fragmentcontentid;//shows the first page by default fragmenttransaction ft = Fragmentactivity.getsupportfragmentmanager (). begintransactiOn (); Ft.add (Fragmentcontentid, Fragments.get (2)); Ft.commit (); This.rgs.setOnCheckedChangeListener (this);} /** * Monitor RadioButton change time, when selected different RadioButton will start this function */public void oncheckedchanged (radiogroup group, int checkedid) {for ( int i = 0; I < Rgs.getchildcount (); i++) {if (Rgs.getchildat (i). GetId () = = Checkedid) {Fragment Fragment = Fragments.get (i); Fragmenttransaction ft = obtainfragmenttransaction (i); Getcurrentfragment (). OnPause (); Pause the current tabif (fragment.isadded ()) {fragment.onresume ();//Start the Target tab of the Onresume ()} else {Ft.add (Fragmentcontentid, fragment);} ShowTab (i); Show Target Tabft.commit ();}}} Public Fragment getcurrentfragment () {return fragments.get (currenttab);} private void ShowTab (int idx) {for (int i = 0; i < fragments.size (); i++) {Fragment Fragment = Fragments.get (i); Fragmenttransaction ft = obtainfragmenttransaction (idx), if (idx = = i) {ft.show (fragment);} else {ft.hide (fragment);} Ft.commit ();} Currenttab = idx; Update Target tab is current Tab}private fragmenttransaction obtainfragmenttransaction(int index) {fragmenttransaction ft = Fragmentactivity.getsupportfragmentmanager (). BeginTransaction ();//Set Toggle Animation if (Index > Currenttab) {ft.setcustomanimations (r.anim.slide_left_in, r.anim.slide_left_out);} else {ft.setcustomanimations ( R.anim.slide_right_in,r.anim.slide_right_out);} return ft;}}

The codes for Fragmenthistory, Fragmentanalysis, Fragmentpedometer, FRAGMENTPK, and Fragmentset are:

Package Com.example.changepage1;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * This is a fragment of the analysis of seven-day steps * Author: Li Wu Super   email:[email Protected] * date:2015-1-2 * Time: PM 2:39 */public class Fragmentanalysis extends Fragment{private View View, @Overridepublic view Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate (R.layout.analysis, container, false); return view;}}

Package Com.example.changepage1;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * This is a fragment of history * Author: Li Wu Super   Email:[email Protected] * date:2015-1-2 * Time: PM 2:39 */public Class Fragmenthistory extends fragment{private view view; @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate (R.layout.history, container, false); return view;}}

Package Com.example.changepage1;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * This is the fragment of the step * Author: Li Wu Super   Email:[email Protected] * date:2015-1-2 * Time: PM 2:39 */public class Fragmentpedometer extends Fragment{private View vie W; @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate (R.layout.pedometer, container, false); return view;}}

Package Com.example.changepage1;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * This is the fragment of the step * Author: Li Wu Super   Email:[email Protected] * date:2015-1-2 * Time: PM 2:39 */public Class FRAGMENTPK extends Fragment{private view view; @Over Ridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate (r.layout.pk, container, false); return view;}}

Package Com.example.changepage1;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * This is the set fragment * Author: Li Wu Super   Email:[email Protected] * date:2015-1-2 * Time: PM 2:39 */public Class Fragmentset extends Fragment{private view view; @Ove Rridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view = Inflater.inflate (R.layout.set, container, false); return view;}}

The complete demo of this example is in:

http://download.csdn.net/detail/a296777513/8320619

Android-based pedometer (pedometer) (i)--fragment page jump

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.