Android Custom Component Series "10"-navigation bar with Viewpager sliding

Source: Internet
Author: User

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



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.