简体   繁体   中英

Forward touch events from custom View to ViewPager

Let's say I have a ViewPager that takes 50% of screen height and a simple View (or other component) that takes other half of my screen. When I swipe on the View component I want my interaction to be visible on the ViewPager as if I was swiping on the ViewPager itself.

Any ideas how to implement this? I have tried overriding onTouchEvent in the View and then triggering onTouchEvent in the ViewPager, but that did not seem to work.

I found the solutions myself. Here is how to do it:

1) Create a custom Class for your View that is instance of ViewGroup and overrides onInterceptTouchEvent(). Make sure that children of this ViewGroup return true onTouchEvent().

More explanation here: onInterceptTouchEvent only gets ACTION_DOWN

2) Inside onInterceptTouchEvent(MotionEvent ev) you have to forward the event to viewPager by calling viewPager.dispatchTouchEvent(ev)

You can use special methods in ViewPager class:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM