How to intercept touch events from ViewPager.OnPageChangeListener

20,302

Solution 1

This is how I handle this (my entire class) :

public class SelectiveViewPager extends ViewPager {
private boolean paging = true;

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

public SelectiveViewPager(Context context, AttributeSet attributeSet){
    super(context, attributeSet);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    if (paging) {
        return super.onInterceptTouchEvent(e);
    }

    return false;
}

public void setPaging(boolean p){ paging = p; }

}

The boolean paging gives you the option to turn this on and off, if you need it. If you don't need, just return false. If you aren't doing anything special in onTouch, you don't need to override it.

Solution 2

return true; in your onInterceptTouchEvent();

Share:
20,302
Felipe Mosso
Author by

Felipe Mosso

Updated on July 09, 2022

Comments

  • Felipe Mosso
    Felipe Mosso almost 2 years

    I'm trying to disallow my ViewPager from scrolling between tabs. I have the following situation:

            public class MyClass extends ViewPager implements ViewPager.OnPageChangeListener, OnTouchListener {
    
                 public MyClass() {
                   setOnTouchListener(this);
                 }
    
    @Override
        public void onPageScrollStateChanged(int state) {
            Log.d("Testing", TAG + " onPageScrollStateChanged");
        }
    
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Log.d("Testing", TAG + " onPageScrolled");
        }
    
        @Override
        public void onPageSelected(int position) {
            Log.d("Testing", TAG + " onPageSelected");
        }
    
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                Log.d("Testing", "intercept");
                    return false;
            }
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.d("Testing", "touch2");
                        return false;
                }
    
            }
    

    I have the logs from onPageScrolled and onPageScrollStateChanged normally, but I do not have logs from onInterceptTouchEvent and onTouch. What am I doing wrong?

  • Felipe Mosso
    Felipe Mosso over 11 years
    I'm creating my view pager inflating by xml. This is an resumed version of myClass. But the methods from OnPageChangeListener are running normally.
  • gwvatieri
    gwvatieri over 11 years
    That class doesn't even compile. My point is that constructor must be defined in a proper way according to the way you instantiate your custom view. Anyway it could depend on the views hierarchy, for instance, is there any view/viewgroup that could intercept the touch event earlier?
  • Felipe Mosso
    Felipe Mosso about 11 years
    Yes, this may work.. but the difference is that my class implements ViewPager.OnPageChangeListener. I think this is the problem because onPageScrollStateChanged, onPageScrolled and onPageSelected might use touchEvent, so all touchEvent caught by my application go to these methods. What's your opinion about that?
  • Felipe Mosso
    Felipe Mosso about 11 years
    This is not the problem because I can't even catch the logs from this method.