18

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!

1
  • 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 ! Commented Sep 26, 2019 at 20:03

1 Answer 1

22

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{...}

Sign up to request clarification or add additional context in comments.

1 Comment

setOnTouchListener is never trigged

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.