Viewpager, sliding the components up and down, Android up and down Verticalpager

Source: Internet
Author: User

Package Com.zhulin.android.atools;import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.view.motionevent;import Android.view.velocitytracker;import Android.view.View; Import Android.view.viewgroup;import Android.view.animation.interpolator;import Android.widget.scroller;import Android.widget.toast;public class Verticalpager extends Viewgroup{private Scroller mscroller;private Context mcontext; Public Verticalpager (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext=context;mscroller=new Scroller (context);//mscroller=new Scroller (Mcontext, New Interpolator () {////@Override//public float Getinterpolation (float input) {//return 300;//}//});} @Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {int totalheight=0;int count=getchildcount () ; for (int i=0;i<count;i++) {View childview=getchildat (i);//int measureheight=childview.getmeasuredheight ();//int Measurewidth=childview.getmeasuredwidth (); ChildviEw.layout (L, Totalheight, R, Totalheight+b); totalheight+=b;}} Private Velocitytracker mvelocitytracker; @Overrideprotected void onmeasure (int widthmeasurespec, int HEIGHTMEASURESPEC) {int width=measurespec.getsize (WIDTHMEASURESPEC); int Height=measurespec.getsize ( HEIGHTMEASURESPEC); int count=getchildcount (); for (int i=0;i<count;i++) {getchildat (i). Measure (width, height);} Setmeasureddimension (width, height);} private int mlastmotiony; @Overridepublic boolean ontouchevent (Motionevent event) {if (mvelocitytracker==null) { Mvelocitytracker=velocitytracker.obtain ();} Mvelocitytracker.addmovement (event); int action=event.getaction (); float y=event.gety (); switch (action) {case MotionEvent.ACTION_DOWN:if (!mscroller.isfinished ()) {mscroller.abortanimation ();} mlastmotiony= (int) y; LOG.D ("Montion", "" "+getscrolly ()); Break;case MotionEvent.ACTION_MOVE:int deltay= (int) (mlastmotiony-y); Scrollby (0, DeltaY);//mscroller.startscroll (0, getscrolly (), 0, DeltaY); invalidate (); mlastmotiony= (int) y;break;case Motionevent.aCtion_up:if (mvelocitytracker!=null) {mvelocitytracker.recycle (); mvelocitytracker=null;} if (getscrolly () <0) {mscroller.startscroll (0,-400, 0, 400);} else if (getscrolly () > (GetHeight () * (Getchildcount ()-1)) {View Lastview=getchildat (Getchildcount ()-1); Mscroller.startscroll (0,lastview.gettop () +300, 0,-300);} Else{int position=getscrolly ()/getheight (); int mod=getscrolly ()%getheight (); if (Mod>getheight ()/3) {View Positionview=getchildat (position+1); Mscroller.startscroll (0, Positionview.gettop ()-300, 0, +300);} Else{view Positionview=getchildat (position); Mscroller.startscroll (0, Positionview.gettop () +300, 0,-300);}} Invalidate (); Break;//case motionevent.action_mask://if (getscrolly () <0) {//mscroller.startscroll (0, 0, 0, 0);//} else if (getscrolly () > (GetHeight () * (Getchildcount ()-1)) {//}//invalidate ();//break;} return true;} @Overridepublic void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {scrollTo (0), Mscroller.getcurry ());} Else{}}}

XML layout

<?

XML version= "1.0" encoding= "UTF-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android "Android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "> & Lt;com.zhulin.android.atools.verticalpager android:id= "@+id/vertypager1" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <textview android:background= "@drawable/a1" Android:id= "@+id/textview1" android:layout_width= "match_parent" android:layout_height= "100DP" android:text= "TextView1111"/> <textview android:background= "@drawable/a2" Android Oid:id= "@+id/textview2" android:layout_width= "fill_parent" android:layout_height= "100DP" android:text= "TextView2222"/> <textview android:background= "@drawable/a3" an Droid:id= "@+id/textview3" Android:layout_width= "Fill_parent" android:layout_height= "100DP" android:text= "TextView2222"/&G T <textview android:background= "@drawable/a4" android:id= "@+id/textview4" android:layou T_width= "Fill_parent" android:layout_height= "100DP" android:text= "TextView2222"/> </com.zh Ulin.android.atools.vertypager></linearlayout>



Viewpager, sliding the components up and down, Android up and down Verticalpager

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.