android —— 自定义控件 竖向的ViewPager,上下滑动的组件,android上下滑动 VerticalPager


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;  
//          }  
//      });  
          
    }  
  
    @Override  
    protected 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;  
  
    @Override  
    protected 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;  
    @Override  
    public 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;  
    }  
      
    @Override  
    public void computeScroll() {  
        super.computeScroll();  
          
        if(mScroller.computeScrollOffset()){  
            scrollTo(0, mScroller.getCurrY());  
        }else{  
              
        }  
    }  
  
}  

xml布局
[java] view plaincopy
<?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" >  
  
    <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:id="@+id/textView2"  
            android:layout_width="fill_parent"  
            android:layout_height="100dp"  
            android:text="TextView2222" />  
          
         <TextView  
             android:background="@drawable/a3"  
            android:id="@+id/textView3"  
            android:layout_width="fill_parent"  
            android:layout_height="100dp"  
            android:text="TextView2222" />  
           
          <TextView  
             android:background="@drawable/a4"  
            android:id="@+id/textView4"  
            android:layout_width="fill_parent"  
            android:layout_height="100dp"  
            android:text="TextView2222" />  
    </com.zhulin.android.atools.VertyPager>  
  
</LinearLayout> 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值