Android-Developed custom Horizontalscrollview view for imitation viewpager effects

Source: Internet
Author: User

The development process, need to achieve horizontalscrollview and viewpager effect, so directly rewrite the Horizontalscrollview to achieve the effect of Viewpager.

The actual following:

(1) Custom Horizontalscrollview class: Apphorizontalscrollview implementation:

 PackageCom.czm.ui.view;Importjava.util.ArrayList;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.HorizontalScrollView;/*** * Application Details page Custom Horizontalscrollview view (imitation Viewpager effect) *@authorcaizhiming **/ Public classApphorizontalscrollviewextendsHorizontalscrollview {/*** Data Definition*/    Private intSubchildcount = 0; PrivateViewGroup FirstChild =NULL; Private intDownx = 0; Private intcurrentpage = 0; PrivateArraylist<integer> viewlist =NewArraylist<integer>(); /*** Construction Method *@authorcaizhiming*/     PublicApphorizontalscrollview (Context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle);    Init (); }     PublicApphorizontalscrollview (Context context, AttributeSet attrs) {Super(context, attrs);    Init (); }     PublicApphorizontalscrollview (Context context) {Super(context);    Init (); }    Private voidinit () {sethorizontalscrollbarenabled (false);//set the original scroll invalid} @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) {        Super. Onmeasure (Widthmeasurespec, Heightmeasurespec);    Getchildinfo (); }    /*** Get child View information *@authorcaizhiming*/     Public voidGetchildinfo () {FirstChild= (viewgroup) getchildat (0); if(FirstChild! =NULL) {Subchildcount=Firstchild.getchildcount ();  for(inti = 0; i < Subchildcount; i++) {                if((View) Firstchild.getchildat (i)). GetWidth () > 0) {Viewlist.add ((View) Firstchild.getchildat (i)). GetLeft ()); }            }        }    }    /*** Touch Listening time *@authorcaizhiming*/@Override Public Booleanontouchevent (motionevent ev) {Switch(Ev.getaction ()) { CaseMotionEvent.ACTION_DOWN:downX= (int) Ev.getx ();  Break;  CaseMotionevent.action_move: Break;  Casemotionevent.action_up: CaseMotionevent.action_cancel: {if(Math.Abs ((Ev.getx ()-downx)) > GetWidth ()/4) {                if(Ev.getx ()-downx > 0) {smoothscrolltoprepage (); } Else{smoothscrolltonextpage (); }            } Else{smoothscrolltocurrent (); }            return true; }        }        return Super. ontouchevent (EV); }    /*** Swipe to current page *@authorcaizhiming*/    Private voidsmoothscrolltocurrent () {Smoothscrollto (Viewlist.get (currentpage)-10, 0); }    /*** Swipe to next page *@authorcaizhiming*/    Private voidSmoothscrolltonextpage () {if(CurrentPage < SubChildCount-1) {currentpage++; Smoothscrollto (Viewlist.get (currentpage)-10, 0); }    }    /*** Swipe to previous page *@authorcaizhiming*/    Private voidSmoothscrolltoprepage () {if(CurrentPage > 0) {currentpage--; Smoothscrollto (Viewlist.get (currentpage)-10, 0); }    }    /*** Swipe to next page *@authorcaizhiming*/     Public voidNextPage () {smoothscrolltonextpage (); }    /*** Swipe to previous page *@authorcaizhiming*/     Public voidPrepage () {smoothscrolltoprepage (); }    /*** Jump to the specified page * *@paramPage *@authorcaizhiming*/     Public BooleanGotoPage (intpage) {        if(Page > 0 && page < subChildCount-1) {Smoothscrollto (Viewlist.get (page),0); CurrentPage=page; return true; }        return false; }}

(2) The following methods are used in the UI configuration file xml:

<Com.czm.ui.view.AppHorizontalScrollView Android:id= "@+id/horizontalscrollview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:visibility= "Gone"Android:scrollbars= "None" > <LinearLayout Android:id= "@+id/llcoverlist"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:padding= "10DP"Android:background= "#DDDDDD"android:orientation= "Horizontal" > </LinearLayout> </com.czm.ui.view.apphorizontalscrollview&gt ;

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.