From a8a7c92b3689cb90d72d03c0162bca848b19c392 Mon Sep 17 00:00:00 2001 From: Adam Cohen Date: Mon, 28 Feb 2011 17:20:44 -0800 Subject: Adding mouse scroll wheel support to StackView Change-Id: I8ae5039606b3080059cea579547f6c61586641e3 --- core/java/android/widget/StackView.java | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/java/android/widget/StackView.java b/core/java/android/widget/StackView.java index e6cf31e..bab469b 100644 --- a/core/java/android/widget/StackView.java +++ b/core/java/android/widget/StackView.java @@ -33,6 +33,7 @@ import android.graphics.Region; import android.graphics.TableMaskFilter; import android.util.AttributeSet; import android.util.Log; +import android.view.InputDevice; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; @@ -114,6 +115,8 @@ public class StackView extends AdapterViewAnimator { private static final int MIN_TIME_BETWEEN_INTERACTION_AND_AUTOADVANCE = 5000; + private static long MIN_TIME_BETWEEN_SCROLLS = 100; + /** * These variables are all related to the current state of touch interaction * with the stack @@ -137,6 +140,7 @@ public class StackView extends AdapterViewAnimator { private StackSlider mStackSlider; private boolean mFirstLayoutHappened = false; private long mLastInteractionTime = 0; + private long mLastScrollTime; private int mStackMode; private int mFramePadding; private final Rect stackInvalidateRect = new Rect(); @@ -565,6 +569,38 @@ public class StackView extends AdapterViewAnimator { } } + @Override + public boolean onGenericMotionEvent(MotionEvent event) { + if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { + switch (event.getAction()) { + case MotionEvent.ACTION_SCROLL: { + final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); + if (vscroll < 0) { + pacedScroll(false); + return true; + } else if (vscroll > 0) { + pacedScroll(true); + return true; + } + } + } + } + return super.onGenericMotionEvent(event); + } + + // This ensures that the frequency of stack flips caused by scrolls is capped + private void pacedScroll(boolean up) { + long timeSinceLastScroll = System.currentTimeMillis() - mLastScrollTime; + if (timeSinceLastScroll > MIN_TIME_BETWEEN_SCROLLS) { + if (up) { + showPrevious(); + } else { + showNext(); + } + mLastScrollTime = System.currentTimeMillis(); + } + } + /** * {@inheritDoc} */ -- cgit v1.1