16

I'm migrating my ViewPager to the new ViewPager2. Unfortunately with this new class, the setOnTouchListener is never called.

mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                LogUtils.LOGD("XXXX", "motionEvent.getAction()=" + motionEvent.getAction());
                ...
                return false;
            }
        });

Do you know how can I fix it?

Thank you very much guys!

anthony
  • 7,653
  • 8
  • 49
  • 101
  • i had this issue too and it don't know why, and i wanted to extend the viewpager2 to override the touch methods but i couldn't because the viewpager2 is final ! – Ahmed Elshaer Sep 26 '19 at 20:03

1 Answers1

20

Because ViewPager2 is a ViewGroup, the final target is the recyclerview in it. The setOnTouchListener not called is because recyclerview intercepts the event and calls the onTouchEvent first.

The right way to add customised onTouch logic is to call mViewPager.getChildAt(0).setOnTouchListener{...}

Leon Wu
  • 321
  • 3
  • 5