Android自定义ViewPager:水平滑动弹性效果,侧滑刷新加载的ViewPager

效果:

  • 1.当viewpger为第一页时向右滑动viewpager向右移动;
  • 2.当viewpger为最后一页时向左滑动是viewpger向左移动;

首先先来看看效果图:

是不是你想要的效果呢?
思路:
1.继承viewpger
2.重写onPageScrolled方法,判断是能滑动
3.重写onTouchEvent方法,处理滑动事件

上代码:
setp1:继承Viewpager,从写构造方法

public class FlexibleViewPager extends ViewPager {
    public FlexibleViewPager(Context context) {
        super(context);
    }

    public FlexibleViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

}

setp2 : 重写onPageScrolled方法,判断是否能滑出

 @Override
    protected void onPageScrolled(int position, float offset, int offsetPixels) {//监听viewpager是否是第一页或最后一页
        if (getAdapter() == null && getAdapter().getCount() == 0) {
            isMoveLeft = false;
            isMoveRight = false;
        } else if (position == 0 && offset == 0 && offsetPixels == 0) {//当前为首页,左边能滑出
            isMoveLeft = true;
        } else if (position == getAdapter().getCount() - 1 && offset == 0 && offsetPixels == 0) {
            isMoveRight = true;
        } else {
            isMoveLeft = false;
            isMoveRight = false;
        }
        if (normal.isEmpty() || normal.top - normal.bottom == 0) {
            normal.set(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());//记录原来view的位置
        }
        super.onPageScrolled(position, offset, offsetPixels);
    }

setp3:重写onTouchEvent方法,处理滑动事件

 @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (x == 0) {
                    x = ev.getX();//记录位置
                }
                xMove = (int) (x - ev.getX()) / 2;//计算移动距离
                if (isMoveLeft && xMove <= 0||isMoveRight && xMove >= 0) {//移动位置
                    this.layout(-xMove, normal.top, normal.right - xMove, normal.bottom);
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isMoveLeft || isMoveRight) {
                    animation(xMove);//还原动画
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

附上全部代码:

/**
 * Created by wcb on 2017/4/19.
 */

public class FlexibleViewPager extends ViewPager {
    private static final String TAG = "FlexibleViewPager::::";
    private boolean isMoveLeft = true;//左边是否能移动
    private boolean isMoveRight;
    private Rect normal = new Rect();//记录原来的位置
    private OnRefreshListener listener;
    float x = 0;//记录开始触摸的位置
    int xMove = 0;//移动的距离

    public FlexibleViewPager(Context context) {
        super(context);
    }

    public FlexibleViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (x == 0) {
                    x = ev.getX();//记录位置
                }
                xMove = (int) (x - ev.getX()) / 2;//计算移动距离
                Log.e(TAG, "xMove:" + xMove + "isMoveLeft:" + isMoveLeft + "isMoveRight:" + isMoveRight);
                if (isMoveLeft && xMove <= 0 || isMoveRight && xMove >= 0) {//移动位置
                    this.layout(-xMove, normal.top, normal.right - xMove, normal.bottom);
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (isMoveLeft || isMoveRight) {
                    animation(xMove);//还原位置
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onPageScrolled(int position, float offset, int offsetPixels) {//监听viewpager是否是第一页或最后一页
        if (getAdapter() == null && getAdapter().getCount() == 0) {
            isMoveLeft = false;
            isMoveRight = false;
        } else if (position == 0 && offset == 0 && offsetPixels == 0) {
            isMoveLeft = true;
        } else if (position == getAdapter().getCount() - 1 && offset == 0 && offsetPixels == 0) {
            isMoveRight = true;
        } else {
            isMoveLeft = false;
            isMoveRight = false;
        }
        if (normal.isEmpty() || normal.top - normal.bottom == 0) {
            normal.set(this.getLeft(), this.getTop(), this.getRight(), this.getBottom());//viewpager记录原来位置
        }
        super.onPageScrolled(position, offset, offsetPixels);
    }

    /**
     * 还原动画
     **/
    public void animation(int moveX) {
        if (listener != null) {
            if (moveX > getWidth() / 6) {//滑动的距离超过屏幕的1/6才回调
                listener.onLoadMore();
            } else if (moveX < -getWidth() / 6) {
                listener.onRefresh();
            }
        }
        x = 0;
        TranslateAnimation ta = new TranslateAnimation(this.getX(), normal.left, 0, 0);
        ta.setDuration(200);
        this.startAnimation(ta);
        this.layout(normal.left, normal.top, normal.right, normal.bottom);
    }

    public void setOnRefreshListener(OnRefreshListener listener) {
        this.listener = listener;
    }

    public interface OnRefreshListener {
        void onRefresh();

        void onLoadMore();
    }

}

git地址:https://github.com/chuangbin/FlexibleViewPager

你可能感兴趣的:(JAVA,android)