Yesterday in the use of viewpager to achieve sliding navigation, found that the navigation bar effect is to follow the viewpager of the sliding movement, just began to think about, the feeling can be used to implement the animation, but this slide with the finger changes at all times, seemingly not, and then search the Internet, find an open source code, Results open a look surprised, so simple effect code incredibly has more than 300 lines, too occupy the mobile phone storage space! Then I simply rewrite ViewGroup using the Scrollto method to achieve a bit, 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 Sun Cockroach * */public class Viewpagertab extends VIEWGR Oup{private Viewpager mviewpager;private Pagelistener mpagelistener = new Pagelistener ();p rivate int mWidth;private int m Height; 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). LA Yout (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.setonpagechangelistener (Mpagelistener);} Private class Pagelistener implements Onpagechangelistener {@Overridepublic void onpagescrolled (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:
Full source code at GitHub address: https://github.com/lxqxsyu/SlideUpDownViewPage