Add support for horizontal scrolling within a ViewPager (ICS-only)
Change-Id: I7ceb2eb65536a2dc7a9323202c1999071cae237a
diff --git a/v4/Android.mk b/v4/Android.mk
index 95e16c8..e35e207 100644
--- a/v4/Android.mk
+++ b/v4/Android.mk
@@ -47,12 +47,21 @@
# A helper sub-library that makes direct use of Honeycomb MR2 APIs.
include $(CLEAR_VARS)
LOCAL_MODULE := android-support-v4-honeycomb-mr2
-LOCAL_SDK_VERSION := current
+LOCAL_SDK_VERSION := 13
LOCAL_SRC_FILES := $(call all-java-files-under, honeycomb_mr2)
include $(BUILD_STATIC_JAVA_LIBRARY)
# -----------------------------------------------------------------------
+# A helper sub-library that makes direct use of Ice Cream Sandwich APIs.
+include $(CLEAR_VARS)
+LOCAL_MODULE := android-support-v4-ics
+LOCAL_SDK_VERSION := current
+LOCAL_SRC_FILES := $(call all-java-files-under, ics)
+include $(BUILD_STATIC_JAVA_LIBRARY)
+
+# -----------------------------------------------------------------------
+
# Here is the final static library that apps can link against.
include $(CLEAR_VARS)
LOCAL_MODULE := android-support-v4
@@ -62,7 +71,8 @@
android-support-v4-eclair \
android-support-v4-froyo \
android-support-v4-honeycomb \
- android-support-v4-honeycomb-mr2
+ android-support-v4-honeycomb-mr2 \
+ android-support-v4-ics
include $(BUILD_STATIC_JAVA_LIBRARY)
# Include this library in the build server's output directory
diff --git a/v4/ics/android/support/v4/view/ViewCompatICS.java b/v4/ics/android/support/v4/view/ViewCompatICS.java
new file mode 100644
index 0000000..c6040dc
--- /dev/null
+++ b/v4/ics/android/support/v4/view/ViewCompatICS.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.support.v4.view;
+
+import android.view.View;
+
+/**
+ * Helper for accessing newer features in View
+ */
+class ViewCompatICS {
+ public static boolean canScrollHorizontally(View v, int direction) {
+ return v.canScrollHorizontally(direction);
+ }
+
+ public static boolean canScrollVertically(View v, int direction) {
+ return v.canScrollVertically(direction);
+ }
+}
diff --git a/v4/java/android/support/v4/view/ViewCompat.java b/v4/java/android/support/v4/view/ViewCompat.java
new file mode 100644
index 0000000..827d5f2
--- /dev/null
+++ b/v4/java/android/support/v4/view/ViewCompat.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.support.v4.view;
+
+import android.view.View;
+
+/**
+ * Helper for accessing newer features in View.
+ */
+public class ViewCompat {
+ interface ViewCompatImpl {
+ public boolean canScrollHorizontally(View v, int direction);
+ public boolean canScrollVertically(View v, int direction);
+ }
+
+ static class BaseViewCompatImpl implements ViewCompatImpl {
+ public boolean canScrollHorizontally(View v, int direction) {
+ return false;
+ }
+ public boolean canScrollVertically(View v, int direction) {
+ return false;
+ }
+ }
+
+ static class ICSViewCompatImpl implements ViewCompatImpl {
+ public boolean canScrollHorizontally(View v, int direction) {
+ return ViewCompatICS.canScrollHorizontally(v, direction);
+ }
+ public boolean canScrollVertically(View v, int direction) {
+ return ViewCompatICS.canScrollVertically(v, direction);
+ }
+ }
+
+ static final ViewCompatImpl IMPL;
+ static {
+ if (android.os.Build.VERSION.SDK_INT >= 14) {
+ IMPL = new ICSViewCompatImpl();
+ } else {
+ IMPL = new BaseViewCompatImpl();
+ }
+ }
+
+ public static boolean canScrollHorizontally(View v, int direction) {
+ return IMPL.canScrollHorizontally(v, direction);
+ }
+
+ public static boolean canScrollVertically(View v, int direction) {
+ return IMPL.canScrollVertically(v, direction);
+ }
+}
diff --git a/v4/java/android/support/v4/view/ViewPager.java b/v4/java/android/support/v4/view/ViewPager.java
index 28a2504..8525a7c 100644
--- a/v4/java/android/support/v4/view/ViewPager.java
+++ b/v4/java/android/support/v4/view/ViewPager.java
@@ -705,10 +705,21 @@
final int pointerIndex = MotionEventCompat.findPointerIndex(ev, activePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
- final int xDiff = (int) Math.abs(x - mLastMotionX);
+ final int dx = (int) (x - mLastMotionX);
+ final int xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final int yDiff = (int) Math.abs(y - mLastMotionY);
+ final int scrollX = getScrollX();
+ final boolean atEdge = (dx > 0 && scrollX == 0) || (dx < 0 && mAdapter != null &&
+ scrollX >= (mAdapter.getCount() - 1) * getWidth() - 1);
if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
+
+ if (atEdge || canScroll(this, false, dx, (int) x, (int) y)) {
+ // Nested view has scrollable area under this point. Let it be handled there.
+ mInitialMotionX = mLastMotionX = x;
+ mLastMotionY = y;
+ return false;
+ }
if (xDiff > mTouchSlop && xDiff > yDiff) {
if (DEBUG) Log.v(TAG, "Starting drag!");
mIsBeingDragged = true;
@@ -916,6 +927,40 @@
}
}
+ /**
+ * Test scrollability within child views of v given a delta of dx.
+ *
+ * @param v View to test for horizontal scrollability
+ * @param checkV Whether the view v passed should itself be checked for scrollability (true),
+ * or just its children (false).
+ * @param dx Delta scrolled in pixels
+ * @param x X coorindate of the active touch point
+ * @param y Y coordinate of the active touch point
+ * @return Delta still left to be scrolled by a parent.
+ */
+ static boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
+ if (v instanceof ViewGroup) {
+ final ViewGroup group = (ViewGroup) v;
+ final int scrollX = v.getScrollX();
+ final int scrollY = v.getScrollY();
+ final int count = group.getChildCount();
+ // Count backwards - let topmost views consume scroll distance first.
+ for (int i = count - 1; i >= 0; i--) {
+ // TODO: Add versioned support here for transformed views.
+ // This will not work for transformed views in Honeycomb+
+ final View child = group.getChildAt(i);
+ if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
+ y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
+ canScroll(child, true, dx, x + scrollX - child.getLeft(),
+ y + scrollY - child.getTop())) {
+ return true;
+ }
+ }
+ }
+
+ return checkV && ViewCompat.canScrollHorizontally(v, -dx);
+ }
+
private class DataSetObserver implements PagerAdapter.DataSetObserver {
@Override
public void onDataSetChanged() {