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> ;