Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.support.v4.view.ViewPager;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- public class SwipeOutViewPager extends ViewPager {
- private float mVelocityX;
- private OnSwipeOutListener mSwipeOutListener;
- public SwipeOutViewPager(Context context) {
- super(context);
- }
- public SwipeOutViewPager(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public void setOnSwipeOutListener(OnSwipeOutListener listener) {
- mSwipeOutListener = listener;
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- if (ev.getAction() == MotionEvent.ACTION_DOWN) {
- mVelocityX = ev.getX();
- }
- return super.onInterceptTouchEvent(ev);
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- if (mSwipeOutListener != null) {
- int last = getAdapter().getCount() - 1;
- boolean lastItem = getCurrentItem() == last;
- boolean firstItem = getCurrentItem() == 0;
- if (firstItem || lastItem) {
- float x = ev.getX();
- switch (ev.getAction()) {
- case MotionEvent.ACTION_MOVE:
- break;
- case MotionEvent.ACTION_UP:
- if (firstItem && x > mVelocityX) {
- //swipe out at start
- mSwipeOutListener.onSwipeOut(false);
- } else if (lastItem && x < mVelocityX) {
- //swipe out at end
- mSwipeOutListener.onSwipeOut(true);
- }
- break;
- }
- } else {
- mVelocityX = 0;
- }
- }
- return super.onTouchEvent(ev);
- }
- public interface OnSwipeOutListener {
- void onSwipeOut(boolean end);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement