Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 392
anthony Avatar asked Aug 18 '19 14:08

anthony


1 Answers

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

like image 126
Leon Wu Avatar answered Oct 05 '22 05:10

Leon Wu