自定义viewpager

本文介绍了一种自定义ScrollerLayout的方法,该方法通过监听触摸事件并利用Scroller类实现视图的平滑滚动。文章详细展示了如何通过判断触摸事件来决定是否拦截子控件的触摸事件,以及如何计算滚动位置。
摘要由CSDN通过智能技术生成
import android.content.Context;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;
import com.zoomy.studio.locker.utils.L;

public class ScrollerLayout extends LinearLayout {

    private Scroller mScroller;

    //判定为拖动的最小移动像素数
    private int mTouchSlop;

    //手机按下时的屏幕坐标
    private float mXDown;

   //手机当时所处的屏幕坐标
    private float mXMove;

    //上次触发ACTION_MOVE事件时的屏幕坐标
    private float mXLastMove;

   //界面可滚动的左边界
    private int leftBorder;

   //界面可滚动的右边界
    private int rightBorder;

    //当前索引
    private int targetIndex;

    private boolean isUp;

    public ScrollerLayout(Context context){
        this(context,null);
    }

    public ScrollerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context,new DecelerateInterpolator(1.2f));
        ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
        setOrientation(HORIZONTAL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed,l,t,r,b);
        L.i("qgl", "ScrollerLayout onLayout change=" + changed);
        if (changed) {
//            int childCount = getChildCount();
//            for (int i = 0; i < childCount; i++) {
//                View childView = getChildAt(i);
//                // 水平布局
//                childView.layout(i * childView.getMeasuredWidth(), 0, (i + 1) * childView.getMeasuredWidth(), childView.getMeasuredHeight());
//            }


            // 初始化左右边界值
            leftBorder = getChildAt(0).getLeft();
            rightBorder = getChildAt(getChildCount() - 1).getRight();

            scrollTo(getWidth(), 0);
            targetIndex = 1;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isUp = false;
                mXDown = ev.getRawX();
                mXLastMove = mXDown;
                L.i("qgl onInterceptTouchEvent ACTION_DOWN  mXDown=" + mXDown + " mXLastMove=" + mXLastMove);
                break;
            case MotionEvent.ACTION_MOVE:
                mXMove = ev.getRawX();
                float diff = Math.abs(mXMove - mXDown);
                mXLastMove = mXMove;
                L.i("qgl onInterceptTouchEvent ACTION_MOVE  mXMove=" + mXMove + " mXLastMove=" + mXLastMove + " diff=" + diff);
                // 当手指拖动值大于TouchSlop值时,认为应该进行滚动,拦截子控件的事件
                if (diff > mTouchSlop) {
                    L.i("qgl","ScrollerLayout 拦截了");
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    float startx = 0;
    float endx = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isUp = false;
                startx = event.getX();
                L.i("qgl  startx=" + startx);
                break;
            case MotionEvent.ACTION_MOVE:
                L.i("qgl","ScrollerLayout ACTION_MOVE onPageScrolled 执行了 position="+targetIndex);
                listener.onPageScrolled(targetIndex);

                mXMove = event.getRawX();
                L.i("qgl onTouchEvent ACTION_MOVE  mXMove=" + mXMove);
                int scrolledX = (int) (mXLastMove - mXMove);
                L.i("qgl onTouchEvent ACTION_MOVE  scrolledX=" + scrolledX + " getScrollX()=" + getScrollX());
                L.i("qgl onTouchEvent ACTION_MOVE  getScrollX() + scrolledX =" + getScrollX() + scrolledX + " leftBorder=" + leftBorder);
                if (getScrollX() + scrolledX < leftBorder) {
                    scrollTo(leftBorder, 0);
                    return true;
                } else if (getScrollX() + getWidth() + scrolledX > rightBorder) {
                    L.i("qgl onTouchEvent ACTION_MOVE  getScrollX() + getWidth() + scrolledX =" + getScrollX() + getWidth() + scrolledX + " rightBorder=" + rightBorder);

                    scrollTo(rightBorder - getWidth(), 0);
                    return true;
                }
                scrollBy(scrolledX, 0);
                L.i("qgl onTouchEvent ACTION_MOVE scrolledX =" + scrolledX);

                mXLastMove = mXMove;
                break;
            case MotionEvent.ACTION_UP:
                endx = event.getX();
                isUp = true;
                L.i("qgl onTouchEvent ACTION_UP mxdown=" + mXDown + "  mXLastMove=" + mXLastMove);
                // 当手指抬起时,根据当前的滚动值来判定应该滚动到哪个子控件的界面
                if(Math.abs(mXLastMove-mXDown) > getWidth() /8){
                    if(mXLastMove < mXDown){
                        targetIndex++;
                    }else {
                        targetIndex--;
                    }
                }
                L.i("qgl onTouchEvent ACTION_UP  targetIndex=" + targetIndex);
                if(targetIndex >= getChildCount()){
                    targetIndex = getChildCount() -1;
                }
                if(targetIndex < 0){
                    targetIndex = 0;
                }
                int dx = targetIndex * getWidth() - getScrollX();
                L.i("qgl onTouchEvent ACTION_UP  dx=" + dx);
                mScroller.startScroll(getScrollX(), 0, dx, 0,300);
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            L.i("qgl","ScrollerLayout computeScroll onPageScrolled 执行了 position="+targetIndex);
            listener.onPageScrolled(targetIndex);
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }else {
            if(isUp) {
                L.i("qgl", "ScrollerLayout onPageSelected 执行了");
                listener.onPageSelected(targetIndex);
            }
        }
    }

    private OnPageChangeListener listener;
    public void addOnPageChangeListener(OnPageChangeListener listener){
        this.listener = listener;
    }
    public interface OnPageChangeListener{
        void onPageScrolled(int position);
        void onPageSelected(int position);
    }

    public  void setCurrentItem(int position){
        scrollTo(getWidth()*position, 0);
        targetIndex = position;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值