Android-based pedometer (pedometer)--an indicator that follows the interface slide

Source: Internet
Author: User

Pedometer (pedometer) the entire project source code, interested friends can download to see (remember to help younger brother on GitHub to play a Star ~)

Https://github.com/296777513/pedometer

Today, the indicator is sliding with the interface, which may be less intuitive, I'll attach several:


, the middle part is a viewpager (can slide the page), you can see the top of the screen, the font below an indicator,

As the page slides to the right, the indicator also moves to the right as the interface moves, and the font changes to blue when the page stops on the second page.

Come down and post the main code for everyone:

Package Com.example.histogram;import Java.util.arraylist;import Java.util.list;import Com.example.changepage1.R; Import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.color;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.FragmentPagerAdapter; Import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.util.displaymetrics;import Android.view.display;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import android.widget.linearlayout.layoutparams;/** *  This is the fragment of the step Author: Li Wu Super * email:[email protected] date:2015-1-8 time: PM 6:39 */public class FRAGMENTPK extends Fragment {private View view;private int currentpage1_2;//This is the general private ImageView of the screen bar;//the line that moves with the screen private TextView title1, Title2 ;//private Viewpagerpage;//sliding Change Interface private fragmentpageradapter adapter;private list<fragment> fragments;private int currentpageindex;//Current page @overridepublic View oncreateview (layoutinflater inflater, ViewGroup container,Bundle Savedinstancestate) {view = Inflater.inflate (r.layout.pk, container, false); init (); Inittabline (); return view;} @SuppressLint ("Newapi") private void Inittabline () {bar = (ImageView) View.findviewbyid (r.id.bar);D isplay Display = Getactivity (). GetWindow (). Getwindowmanager (). Getdefaultdisplay ();D isplaymetrics dmetrics = new Displaymetrics ();// Phone screen width and height display.getrealmetrics (dmetrics),//into the dmetrics into the current phone width and height currentpage1_2 = (DMETRICS.WIDTHPIXELS/2)-dip2px ( Getactivity (), 60); Linearlayout.layoutparams LP = (layoutparams) bar.getlayoutparams (); lp.width = Currentpage1_2;lp.leftmargin = dip2px ( Getactivity (), BAR.SETLAYOUTPARAMS (LP);} /** * Initialize data */private void init () {title1 = (TextView) View.findviewbyid (R.ID.PK1) title2 = (TextView) View.findviewbyid (R . id.pk2); Title1.settextcolor(Color.parsecolor ("#6DCAEC"));p age = (Viewpager) View.findviewbyid (r.id.viewpager); fragments = new arraylist< Fragment> (); Fragments.add (new Fragmentpk_1 ()); Fragments.add (new Fragmentpk_2 ());//adapter Viewpager for instantiating adapter = new Fragmentpageradapter (Getactivity (). Getsupportfragmentmanager ()) {@Overridepublic int getcount () {//TODO Auto-generated method Stubreturn fragments.size ();} @Overridepublic Fragment getItem (int arg0) {return fragments.get (arg0);}}; Page.setadapter (adapter);//set adapter for Viewpager//Set listener Page.setonpagechangelistener for Viewpager (new Onpagechangelistener () {@SuppressLint ("Resourceascolor") @Overridepublic void onpageselected (int position) {switch ( Position) {Case 0:title2.settextcolor (Color.parsecolor ("#000000")); Title1.settextcolor (Color.parsecolor ("#6DCAEC "), Break;case 1:title1.settextcolor (Color.parsecolor (" #000000 ")); Title2.settextcolor (Color.parsecolor (" #6DCAEC "); break;} CurrentPageIndex = position;} @Overridepublic void onpagescrolled (int position, float Positionoffset, int positionoffsetpx) {linearlayout.layoutparams LP = (layoutparams) bar.getlayoutparams (); if (CurrentPageIndex = = 0 & amp;& position = = 0) {//This is from the first page to the second page lp.leftmargin = (int) (DIP2PX (Getactivity (), x) + CurrentPageIndex * CURRENTPAG E1_2 + currentpage1_2* positionoffset);} else if (CurrentPageIndex = = 1 && Position = = 0) {lp.leftmargin = (int) (DIP2PX (Getactivity (), +) + Currentpageind ex * currentpage1_2 + currentpage1_2* (positionOffset-1));} BAR.SETLAYOUTPARAMS (LP);} @Overridepublic void onpagescrollstatechanged (int arg0) {//TODO auto-generated Method stub}}); private int dip2px (context context, float Dipvalue) {final float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (scale * dipvalue + 0.5f);}}

There is also the corresponding XML page code:

<?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:orientation=" vertical " > <linearlayout android:layout_width= "match_parent" android:layout_height= "53DP" Android:ori entation= "vertical" > <linearlayout android:layout_width= "match_parent" Android:layout_                 height= "50DP" android:gravity= "center" android:orientation= "Horizontal" > <textview Android:id= "@+id/pk1" android:layout_width= "Wrap_content" Android:layout_hei                ght= "Match_parent" android:layout_marginleft= "60DP" android:layout_weight= "1" android:gravity= "Center" android:text= "multi-person PK" android:textsize= "20DP"/> <t Extview Android:layoUt_width= "1DP" android:layout_height= "10DP" android:background= "@android: Color/darker_gray"                /> <textview android:id= "@+id/pk2" android:layout_width= "Wrap_content" android:layout_height= "Match_parent" android:layout_marginright= "60DP" Android:la yout_weight= "1" android:gravity= "center" android:text= "group PK" Android:textsize = "20DP"/> </LinearLayout> <imageview android:id= "@+id/bar" android:layout _width= "Match_parent" android:layout_height= "3DP" android:background= "@android: Color/holo_blue_brigh T "/> </LinearLayout> <textview android:layout_width=" Match_parent "android:layout_height= "1DP" android:background= "@android: Color/darker_gray"/> <android.support.v4.view.viewpager Android    : id= "@+id/viewpager"    Android:layout_width= "Match_parent" android:layout_height= "match_parent" > &LT;/ANDROID.SUPPORT.V4.VIEW.V Iewpager></linearlayout>

If you want to demo friends can leave a message, and leave the mailbox, I give hair.

Android-based pedometer (pedometer)--an indicator that follows the interface slide

Related Article

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.