anthony
anthony

Reputation: 7733

ViewPager2: setOnTouchListener() doesn't call

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!

Upvotes: 18

Views: 3479

Answers (1)

Leon Wu
Leon Wu

Reputation: 471

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

Upvotes: 22

Related Questions