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;/*** http://blog.csdn.net/dawanganban * @ 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: https://github.com/lxqxsyu/SlideUpDownViewPage