美文网首页
使用Scroller实现简易版viewPager

使用Scroller实现简易版viewPager

作者: 苏苏苏苏考拉 | 来源:发表于2018-05-24 15:33 被阅读18次

    scrollTo()

    scrollTo()方法是让View相对于初始的位置滚动某段距离.

    scrollBy()

    scrollBy()方法则是让View相对于当前的位置滚动某段距离.
    自己动手实现一个viewGroup可以简单的实现viewPager的功能
    1.自定义view实现viewGroup
    2.获取最小滑动距离

    scrollUp = ViewConfiguration.get(context).getScaledPagingTouchSlop();
    

    3.onMeasure 和 onLayout 排列好子view的位置和宽高
    4.事件拦截,判断用户是否已滑动
    5.onTouchEvent里根据用户手势动作决定滑动方向和距离

    public class ScrollerLayout extends ViewGroup {
    
        private int leftBorder, rightBorder;
        private int scrollUp;
        private Scroller scroller;
        private float actionDown, actionMove, actionLastMove;
    
        public ScrollerLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            scrollUp = ViewConfiguration.get(context).getScaledPagingTouchSlop();
            scroller = new Scroller(context);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            for (int i = 0; i < getChildCount(); i++) {
                measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
            }
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            if (changed) {
                for (int i = 0; i < getChildCount(); i++) {
                    View childAt = getChildAt(i);
                    childAt.layout(i * childAt.getMeasuredWidth(), 0, (i + 1) * childAt.getMeasuredWidth(), childAt.getMeasuredHeight());
                }
                leftBorder = getChildAt(0).getLeft();
                rightBorder = getChildAt(getChildCount() - 1).getRight();
            }
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    actionDown = ev.getRawX();
                    actionLastMove = actionDown;
                    break;
                case MotionEvent.ACTION_MOVE:
                    actionMove = ev.getRawX();
                    actionLastMove = actionMove;
                    if (Math.abs(actionLastMove - actionDown) > scrollUp) {
                        Toast.makeText(getContext(), "滑动了", Toast.LENGTH_LONG).show();
                        return true;
                    }
                    break;
            }
            return super.onInterceptTouchEvent(ev);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE:
                    actionMove = event.getRawX();
                    int vOffset = (int) (actionLastMove - actionMove);
                    if (getScrollX() + vOffset < leftBorder) {
                        scrollTo(leftBorder, 0);
                        return true;
                    } else if (getScrollX() + vOffset + getWidth() > rightBorder) {
                        scrollTo(rightBorder - getWidth(), 0);
                        return true;
                    }
                    scrollTo(getScrollX() + vOffset, 0);
                    actionLastMove = actionMove;
                    break;
                case MotionEvent.ACTION_UP:
                    int dx = (getScrollX() + getWidth() / 2) / getWidth();
                    int offset = dx * getWidth() - getScrollX();
                    scroller.startScroll(getScrollX(), 0, offset, 0);
                    invalidate();
                    break;
            }
            return super.onTouchEvent(event);
        }
    
        @Override
        public void computeScroll() {
            if (scroller.computeScrollOffset()) {
                scrollTo(scroller.getCurrX(), scroller.getCurrY());
                invalidate();
            }
            super.computeScroll();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:使用Scroller实现简易版viewPager

          本文链接:https://www.haomeiwen.com/subject/gkqsjftx.html