ViewPager detect when user is trying to swipe out of bounds

后端 未结 5 1046
生来不讨喜
生来不讨喜 2020-11-27 04:05

I am using the ViewPager consisting of 6 pages to display some data. I want to be able to call a method when the user is at position 0 and tries to swipe to the right (backw

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 04:41

    The answer from Flávio Faria doesn't work for me. The only event I get in onInterceptTouchEvent() is ACTION_DOWN event. So I override the onTouchEvent() method to get it work.

    Here is the code. Note that I only have onSwipeOutAtEnd() in the listener. You can add your code to support swiping left on first vier.

    public class CustomViewPager extends ViewPager {
    
    float mStartDragX;
    OnSwipeOutListener mListener;
    
    
    public void setOnSwipeOutListener(OnSwipeOutListener listener) {
        mListener = listener;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev){
        if(getCurrentItem()==getAdapter().getCount()-1){
            final int action = ev.getAction();
            float x = ev.getX();
            switch(action & MotionEventCompat.ACTION_MASK){
            case MotionEvent.ACTION_DOWN:
                mStartDragX = x;
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                if (x

提交回复
热议问题