I have a Gallery with an adapter which supplies it ScrollViews as its child views. I need to make sure that the touch events are handled correctly and as expected:
Without overriding any methods, the scroll view is the only thing that scrolls - the gallery never scrolls.
So I understand I need to use onInterceptTouchEvent(...) in the gallery to decide to take over a certain series of MotionEvents but I am unsure how to check if the touch is horizontal or vertical in nature.
NestedScrollView is just like ScrollView , but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
Android supports vertical scroll view as default scroll view. Vertical ScrollView scrolls elements vertically. Android uses HorizontalScrollView for horizontal ScrollView.
ScrollView is used to put different or same child views or layouts and the all can be scrolled. ListView is used to put same child view or layout as multiple items. All these items are also scrollable. Simply ScrollView is for both homogeneous and heterogeneous collection.
In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.
OK, after some major fiddling and logcat hacking, here's the solution:
public class SwipeInterceptingGallery extends Gallery {
private float mInitialX;
private float mInitialY;
private boolean mNeedToRebase;
private boolean mIgnore;
public SwipeInterceptingGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SwipeInterceptingGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SwipeInterceptingGallery(Context context) {
super(context);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
if (mNeedToRebase) {
mNeedToRebase = false;
distanceX = 0;
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: {
mIgnore = false;
mNeedToRebase = true;
mInitialX = e.getX();
mInitialY = e.getY();
return false;
}
case MotionEvent.ACTION_MOVE: {
if (!mIgnore) {
float deltaX = Math.abs(e.getX() - mInitialX);
float deltaY = Math.abs(e.getY() - mInitialY);
mIgnore = deltaX < deltaY;
return !mIgnore;
}
return false;
}
default: {
return super.onInterceptTouchEvent(e);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With