Android-based Pedometer (5) -- indicator sliding following the interface

Source: Internet
Author: User

Android-based Pedometer (5) -- indicator sliding following the interface

 

Pedometer is the source code of the entire project. If you are interested, you can download it ~)

Https://github.com/296777513/pedometer

Today, it is not intuitive to say that the indicator slides along the interface. I will first attach several images:

The middle part is a ViewPager (sliding page), you can see the top of the screen, there is a indicator under the font,

When the page slides to the right, the indicator slides dynamically to the right as the interface moves. When the page stops on the second page, the font turns blue.

I will post the main code for you:

 

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 chunk part Author: Li Xiaochao * email: 296777513@qq.com Date: Time: */public class FragmentPK extends Fragment {private View view View; private int currentPage1_2; // This is the screen's general private ImageView bar; // the lines that move with the screen are private TextView title1, title2; // private ViewPager page; // sliding change interface private FragmentPagerAdapter adapter; private List
 
  
Fragments; private int currentPageIndex; // the 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); Display display = getActivity (). getWindow (). getWindowManager (). getdefadisplay display (); DisplayMetrics dMetrics = new DisplayMetrics (); // display the screen width and height of the mobile phone. getRealMetrics (dMetrics); // enter the current mobile phone's width and height into dMetrics currentPage1_2 = (dMetrics. widthPixels/2)-dip2px (getActivity (), 60); LinearLayout. layoutParams lp = (LayoutParams) bar. getLayoutParams (); lp. width = currentPage1_2; lp. leftMargin = dip2px (getActivity (), 60); 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 (#6 DCAEC); page = (ViewPager) view. findViewById (R. id. viewPager); fragments = new ArrayList
  
   
(); Fragments. add (new FragmentPK_1 (); fragments. add (new FragmentPK_2 (); // instantiate the adapter of viewpager = new FragmentPagerAdapter (getActivity (). getsuppfrfragmentmanager () {@ Overridepublic int getCount () {// TODO Auto-generated method stubreturn fragments. size () ;}@ Overridepublic Fragment getItem (int arg0) {return fragments. get (arg0) ;}}; page. setAdapter (adapter); // set the adapter for viewpager // set the listener page for viewpager. setOnPageChangeListener (new OnPageChangeListener () {@ SuppressLint (ResourceAsColor) @ Overridepublic void onPageSelected (int position) {switch (position) {case 0: title2.setTextColor (Color. parseColor (#000000); title1.setTextColor (Color. parseColor (#6 DCAEC); break; case 1: title1.setTextColor (Color. parseColor (#000000); title2.setTextColor (Color. parseColor (#6 DCAEC); break;} currentPageIndex = position ;}@ Overridepublic void onPageScrolled (int position, float positionOffset, int positionOffsetPx) {LinearLayout. layoutParams lp = (LayoutParams) bar. getLayoutParams (); if (currentPageIndex = 0 & position = 0) {// This is from the first page to the second page lp. leftMargin = (int) (dip2px (getActivity (), 60) + currentPageIndex * currentPage1_2 + currentPage1_2 * positionOffset);} else if (currentPageIndex = 1 & position = 0) {lp. leftMargin = (int) (dip2px (getActivity (), 60) + currentPageIndex * 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 );}}
  
 

 

 

 
       
  
 

 

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.