From b1d8e49d7263fb6c9025eb3260c2992c4497a9e6 Mon Sep 17 00:00:00 2001 From: Andrew Chen Date: Wed, 19 Jan 2011 17:05:56 +0000 Subject: [PATCH] Be a little more graceful when scrolling horizontally in a (vertical) scroll view. Not quite to the point of diagonal scrolling, but hopefully closer. --- src/com/fsck/k9/view/ToggleScrollView.java | 42 +++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/com/fsck/k9/view/ToggleScrollView.java b/src/com/fsck/k9/view/ToggleScrollView.java index 386a6cea6..ba348e9c5 100644 --- a/src/com/fsck/k9/view/ToggleScrollView.java +++ b/src/com/fsck/k9/view/ToggleScrollView.java @@ -2,16 +2,19 @@ package com.fsck.k9.view; import android.content.Context; import android.util.AttributeSet; +import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.ScrollView; public class ToggleScrollView extends ScrollView { + private GestureDetector mDetector; private boolean mScrolling = true; public ToggleScrollView(Context context, AttributeSet attrs) { super(context, attrs); + mDetector = new GestureDetector(new YScrollDetector()); } public void setScrolling(boolean enable) @@ -28,6 +31,43 @@ public class ToggleScrollView extends ScrollView @Override public boolean onInterceptTouchEvent(MotionEvent ev) { - return (mScrolling) ? super.onInterceptTouchEvent(ev) : false; + if(!mScrolling) { + return false; + } + + // This doesn't quite get us to diagonal scrolling, but it's somewhat better than what we've + // currently got. This is based on + // http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling + boolean result = super.onInterceptTouchEvent(ev); + if (mDetector.onTouchEvent(ev)) + { + return result; + } + + return false; + } + + // Return false if we're scrolling in the x direction + class YScrollDetector extends GestureDetector.SimpleOnGestureListener + { + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) + { + try + { + if (Math.abs(distanceY) > Math.abs(distanceX)) + { + return true; + } + else + { + return false; + } + } catch (Exception e) + { + // nothing + } + return false; + } } }