带回弹功能的ViewPager

作者: _deadline | 来源:发表于2016-10-22 00:08 被阅读845次

比较简单,直接贴代码了,有兴趣的看看我之前写的几篇自定义View

http://www.jianshu.com/users/25e80ace21b8/latest_articles

<pre>
public class BounceBackViewPager extends ViewPager {

private final static String TAG = BounceBackViewPager.class.getSimpleName();

//摩擦系数
private static final float RATIO = 0.5f;
private float srcX = 0;
private float mTouchSlop = 5f;
private boolean isMoved;

private int mItemCount;
private int mCurrentPos;

private Rect mSrcRect;
private boolean firstTime;

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

public BounceBackViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    initBounceBackViewPager();
    setOverScrollMode(OVER_SCROLL_NEVER);
}

private void initBounceBackViewPager() {
    mItemCount = 0;
    mCurrentPos = 0;
    firstTime = true;
    isMoved = false;
    mSrcRect = new Rect();
}

.
.

//获取viewpager的位置,防止被覆盖,数据,只取一次数据
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    if (firstTime) {
        mSrcRect.left = l;
        mSrcRect.top = t;
        mSrcRect.right = r;
        mSrcRect.bottom = b;
        Log.i(TAG, mSrcRect.left + " / " + mSrcRect.top
                + "  /  " + mSrcRect.right + "  /  " + mSrcRect.bottom);
        firstTime = false;
    }
}

@Override
public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN:

            srcX = ev.getX();
            mCurrentPos = getCurrentItem();

            if (getAdapter() != null) {
                mItemCount = getAdapter().getCount();
            }
            break;

          //松手后回弹,执行回弹动画
        case MotionEvent.ACTION_UP:
            BounceMoveBack();
            isMoved = false;
            break;

        case MotionEvent.ACTION_MOVE:

            float mMoveLength = ev.getX() - srcX;
            srcX = ev.getX();
            
            //以下几个判断,是为了保证除去两个的情况和一个的情况下
            //其余的项没有动画
            if (mItemCount == 1) {
                if (Math.abs(mMoveLength) > mTouchSlop) {
                    Move(mMoveLength);
                }
            } else if (mItemCount >= 2) {
                if (mCurrentPos == 0 && mMoveLength > mTouchSlop) {

                    Move(mMoveLength);

                } else if (mCurrentPos == mItemCount - 1
                                && mMoveLength < -mTouchSlop) {

                    Move(mMoveLength);
                } else {
                    isMoved = false;
                }
            } else {
                isMoved = false;
            }

            if (isMoved) {
                return true; //事件消费了
            }
            break;

        default:
            break;
    }
    return super.onTouchEvent(ev);
}

public void Move(float offset) {

    isMoved = true;
    offsetLeftAndRight((int) (offset * RATIO));
}

public void BounceMoveBack() {

    if (!mSrcRect.isEmpty())
    {
        TranslateAnimation animation = new TranslateAnimation(getLeft(), mSrcRect.left, 0, 0);
        animation.setInterpolator(new OvershootInterpolator(4));
        animation.setDuration(300);
        startAnimation(animation);
        layout(mSrcRect.left, mSrcRect.top, mSrcRect.right, mSrcRect.bottom);
    }
}

}
</pre>

相关文章

网友评论

  • 10e8a366f9e5:如果有效果图就更好了 :blush:
  • 9a9a3c9fbc27:这个动画有bug,就是在第一页滑倒最左边,然后向右滑动在向左滑动,这样重复,会导致view pager的内容显示不全
  • 洛的俠:小编,给个效果图就好了
  • 2bef4dbfab32:如果有效果图就更好了 :blush:
    _deadline: @咕嘟咕嘟冒泡泡 本来用视频录的,然后用视频截了个gif但是,截的时间点错了,然后那时候困的不行就直接上代码了!

本文标题:带回弹功能的ViewPager

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