I have a viewpager, and each page has a scrollview. I want to be able to do scroll vertically and also change page horizontally without raising the finger. The problem is that if I start moving the finger vertically, the scrollview gets the touch events, but if I start moving the finger horizontally, the event is still consumed by scrollview. So I have to raise the finger, and start a new gesture horizontally.
I would like to change this behaviour creating a custom scrollview. If I override onTouchEvent(), I can detect the direction of the event. So I have made something like:
public boolean onTouchEvent(){
if(isVertical()){
return true;
}
return false;
}
My problem is that when I return false, the scrollview stops scrolling, but the touch events are not passed to the viewpager, so I can't swipe to next or previous page.
Related
I have a ViewPager2 with default horizontal swipe-navigaton. Each "page" has a ScrollView so that content can be scrolled vertically. Normally this works well: When the touch movement starts horizontally it is handled by the ViewPager, when it starts vertically it is handled by the ScrollView.
However after a vertical fling gesture, while the ScrollView is scrolling on its own, any touch movement is handled by the ScrollView, whether it starts horizontally or vertically. So users first have to stop the fling before they can swipe to the next page. How can I change this?
In the Gmail app, when swiping between mails, the behaviour is as I expect it.
I finally found a simple solution. I override onInterceptTouchEvent of a parent view of the ViewPager2, there on ACTION_DOWN, I stop the fling of the ScrollView with fling(0).
I have a HorizontalScrollView and I want this to scroll only when the user swipes the screen and it should stop if it does not touch the screen. Now when the user swipes fast and releases the screen the ScrollView moves to the other end.
How can I make it such that it only follows the swipe of the user on screen and stops if the user does not touch the screen?
In addition to Lalit's answer, another way of doing it would be by creating a custom class extending ScrollView and then overriding public void fling (int velocityY);
Just leave the method's body empty.
See it first: https://i.stack.imgur.com/uSgWY.jpg
For now I have the scroll of the button and the scroll of the button clicking the a button with view.animate().translationY(float);
But this is an animation and it's not scrolled by the finger.
Any ideas?
So, there is two ways:
Add onTouchListener and watch ACTION_DOWN, ACTION_MOVE and ACTION_UP events. In DOWN you need to remember touch position, then in MOVE calculate current difference between start position and current. And in UP same with MOVE.
Place your view inside of HorizontalScrollView and system will handle scrolling by itself.
I have a ListView inside a ScrollView. I know I can intercept the touch events using the onInterceptTouchEvent method in the ScrollView. But once the child, i.e. ListView, starts consuming the touch event, onInterceptTouchEvent is not call. My problem is how can I transfer the touch event back to parent(i.e. ScrollView) once the ListView scroll has reached top. I want the scroll to be in continuation.
Suppose I'm scrolling the ListView and have reached the top of the ListView I want the scroll to continue so that I can scroll the parent, i.e. ScrollView. How can I achieve this?
You can check whether you are at the top of the listview, then return false from the touch event.
I have small area of screen where I put ViewPager. While I scroll ViewPager from inside of its area it works fine. But when my finger move beyond the bounds then scrolling is intercepted. This occurs on Android version lower then 4.2. On Android 4.2 scrolling keep on even if the touch was outside the bounds of ViewPager.
How can I fix this bug?
You could override the touch event of the ViewPager, and on an ACTION_DOWN event, call requestDisallowInterceptTouchEvent(true), then set it back to false on ACTION_UP.
This should keep other views from being able to intercept the touch events after you've started a touch event on the ViewPager.