Android custom component series [10]-navigation bar sliding with ViewPager

Source: Internet
Author: User

When ViewPager was used yesterday to implement sliding navigation, the navigation bar was found to be moving with the sliding of ViewPager. At first I thought about it and I felt that it could be implemented through animation, however, this slide changes from time to time with your fingers. It seems that it is not feasible. Then I searched the internet and found an open source code. I was surprised to open the result, there are more than 300 lines of code with such a simple effect, which occupies the storage space of mobile phones! Later, I simply rewritten ViewGroup and implemented it using the scrollTo method. The specific implementation process is as follows:

Package com. example. slideupdownviewpage; import android. content. context; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. util. attributeSet; import android. view. viewGroup;/*** * @ author sunshine intensity **/public class ViewPagerTab extends ViewGroup {private ViewPager mViewPager; private PageListener mPageListener = new PageListener (); private int mWidth; private int mHeight; public ViewPagerTab (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {if (getChildCount ()> 0) {getChildAt (0 ). layout (0, 0, mWidth/3, mHeight) ;}@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec. getSize (widthMeasureSpec); mHeight = MeasureSpec. getSize (heightMeasureSpec);} public void setViewPager (ViewPager viewPager) {this. mViewPager = viewPager; mViewPager. listener (mPageListener);} private class PageListener implements OnPageChangeListener {@ Overridepublic void watermark (int position, float positionOffset, int positionOffsetPixels) {scrollTo (-position * mWidth/3-Math. round (positionOffset * mWidth/3), 0);} @ Overridepublic void onPageSelected (int position) {}@ Overridepublic void onPageScrollStateChanged (int arg0 ){}}}
The effect is as follows:

Complete source code at GitHub address:

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: 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.