I have a HorizontalScrollView
inside my ViewPager
. I set
requestDisallowInterceptTouchEvent(true);
for the HorizontalScrollView
but the ViewPager
is still sometimes intercepting touch events. Is there another command I can use to prevent a View's parent and ancestors from intercepting touch events?
note: the HorizontalScrollView
only occupies half the screen.
So, I know this question is pretty old, but I've come with a solution which I think has some extra advantages over the one posted here. One situation that could happen is that I could have more than one HorizontalScrollView inside my ViewPager (which I do), and then I would need to provide tons of Ids to the ViewPager so it can always check for child touch conflicts.
I analyzed the source from the ViewPager, and found that they use this method called "canScroll" to determine if a child view can be scrolled. Luckily, it wasn't made static nor final, so I could just override it. Inside that method that the "boolean ViewCompat.canScrollHorizontally(View, int)" was called (the one that always returns false for SDK < 14). My solution then was:
So, if you have a different class of View inside your ViewPager that should also receive horizontal drags, just change the customCanScroll(View) so you can return if the touch should not be intercepted or not.
The boolean checkV is true if the current view should also be checked for 'scrollability', that's why we should also check for that.
Hope this helps future issues like this (or if there is a better solution, or an official one from the Android platform, let me know).
This will also work for viewpager within a scrollview: https://stackoverflow.com/a/2655740/969325
I don't know if you've solved the problem already or not. But none of the answers above work properly. So I think it's necessary for those people who might be searching the solutions with suffering.
In fact, the solution is quite simple if you read the official document about handling touch events in ViewGroup.
First you need to understand the usage of
ViewParent.requestDisallowInterceptTouchEvent(boolean), Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).
Then you need to intercept the events and make sure that the parent ViewGroup of your HorizontalScrollView won't dispatch the events any further.
So, set an OnToushListener for you HorizontalScrollView. Detecting the touch down and move events, then set the requestDisallowInterceptTouchEvent(true) to it's parent(ViewPager or sth else) to make sure the events would be handled by the HorizontalScrollView.
Of course you could put these code below into your CustomHorizontalScrollView components so that it can be reused.
Hoping it helps. ;-)
I was having trouble with a small gallery view on the foot of my first page in a viewpager group. On swiping the gallery view focus would jump between moving the gallery items or the viewpager.
Overriding ViewPager as listed above and setting requestDisallowInterceptTouchEvent solved my problem. The pages no longer change when swiping the galleryview. Think its an important solution for those using galleryviews with viewpager.
I found out that if you have a ViewPager in a ScrollView and you want it always to stay in focus, when you touch it and the ScrollView never getting a focus, this does that.
I had the same problem. My solution was:
ViewPager
and add a property calledchildId
.childId
property and set the id of theHorizontalScrollView
.onInterceptTouchEvent()
in the subclass ofViewPager
and if thechildId
property is more than 0 get that child and if the event is inHorizontalScrollView
area return false.Code
In
onCreate()
methodHope this help